阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx虚拟机配置详解

123次阅读
没有评论

共计 6620 个字符,预计需要花费 17 分钟才能阅读完成。

什么是虚拟主机:

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供 www 服务,这样就可以实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响。

Nginx 虚拟机配置详解

nginx 可以实现虚拟主机的配置,nginx 支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机(server_name 来区分虚拟主机——应用:外部网站)
2、基于 ip 的虚拟主机,(一块主机绑定多个 ip 地址)
3、基于端口的虚拟主机(端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

范例:

一、基于域名的虚拟主机

1、配置通过域名区分的虚拟机
[root@linuxidc.com nginx]# cat conf/nginx.conf
 worker_processes 1;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;

server {
 listen 80;
 server_name www.nginx01.com;
 location / {
 root html;
 index index.html index.htm;
 }
 }

server {
 listen 80;
 server_name www.nginx02.com;
 location / {
 root /root/html;
 index index.html index.htm;
 }
 }
 }

2、为 域名为 www.nginx02.com 的虚拟机,创建 index 文件   
 [root@linuxidc.com ~]# mkdir -p /root/html
 [root@linuxidc.com ~]# cd /root/html/
 [root@linuxidc.com html]# vi index.html
 [root@linuxidc.com html]# cat index.html
 <html>
 <p>
 this is my nginx
 </p>
 </html>

3、重新加载配置文件
[root@linuxidc.com nginx]# ./sbin/nginx -s reload

4、客户端配置路由映射
 在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行

10.219.24.26 www.nginx01.com
 10.219.24.26 www.nginx02.com
如图:

Nginx 虚拟机配置详解

5、测试访问

浏览器输入:http://www.nginx01.com/

Nginx 虚拟机配置详解

浏览器输入:http://www.nginx02.com/

Nginx 虚拟机配置详解

 > 成功!

 补充:如果配置不能正常访问,试参考 http://www.linuxidc.com/Linux/2017-06/144827.htm

二、基于 ip 的虚拟主机

1. 一块网卡绑定多个 ip
 [root@linuxidc.com nginx]# ifconfig eth0:1 10.219.24.27
 [root@linuxidc.com nginx]# ifconfig
 eth0 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02
 inet addr:10.219.24.26 Bcast:10.255.255.255 Mask:255.0.0.0
 …
 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02
 inet addr:10.219.24.27 Bcast:10.255.255.255 Mask:255.0.0.0
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 2. 配置通过 ip 区分的虚拟机
 [root@linuxidc.com nginx]# cat conf/nginx.conf
 user root root; #说明:这里的 user 根据 自己的 nginx.conf 文件所在的目录的属主属性而定
worker_processes 1;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;

server {
 listen 10.219.24.26:80;
 server_name www.nginx01.com;
 location / {
 root html;
 index index.html index.htm;
 }
 }

server {
 listen 10.219.24.27:80;
 server_name www.nginx01.com;
 location / {
 root /root/html;
 index index.html index.htm;
 }
 }
 }
 3. reopen nginx
 [root@linuxidc.com nginx]# ./sbin/nginx -s reopen

补充:
— 删除绑定的 vip
 ifconfig eth0:1 10.219.24.27 down

三、基于端口的虚拟主机

配置通过端口区分的虚拟机
[root@linuxidc.com nginx]# cat conf/nginx.conf
 user root root; #说明:这里的 user 根据 自己的 nginx.conf 文件所在的目录的属主属性而定
worker_processes 1;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;

server {
 listen 80;
 server_name www.nginx01.com;
 location / {
 root html;
 index index.html index.htm;
 }
 }

server {
 listen 8080;
 server_name www.nginx01.com;
 location / {
 root /root/html;
 index index.html index.htm;
 }
 }
 }

CentOS 7 下 Nginx 服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx 服务的 SSL 认证和 htpasswd 认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Linux 中安装配置 Nginx 及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9 编译安装 Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-06/144826.htm

什么是虚拟主机:

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供 www 服务,这样就可以实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响。

Nginx 虚拟机配置详解

nginx 可以实现虚拟主机的配置,nginx 支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机(server_name 来区分虚拟主机——应用:外部网站)
2、基于 ip 的虚拟主机,(一块主机绑定多个 ip 地址)
3、基于端口的虚拟主机(端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

范例:

一、基于域名的虚拟主机

1、配置通过域名区分的虚拟机
[root@linuxidc.com nginx]# cat conf/nginx.conf
 worker_processes 1;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;

server {
 listen 80;
 server_name www.nginx01.com;
 location / {
 root html;
 index index.html index.htm;
 }
 }

server {
 listen 80;
 server_name www.nginx02.com;
 location / {
 root /root/html;
 index index.html index.htm;
 }
 }
 }

2、为 域名为 www.nginx02.com 的虚拟机,创建 index 文件   
 [root@linuxidc.com ~]# mkdir -p /root/html
 [root@linuxidc.com ~]# cd /root/html/
 [root@linuxidc.com html]# vi index.html
 [root@linuxidc.com html]# cat index.html
 <html>
 <p>
 this is my nginx
 </p>
 </html>

3、重新加载配置文件
[root@linuxidc.com nginx]# ./sbin/nginx -s reload

4、客户端配置路由映射
 在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行

10.219.24.26 www.nginx01.com
 10.219.24.26 www.nginx02.com
如图:

Nginx 虚拟机配置详解

5、测试访问

浏览器输入:http://www.nginx01.com/

Nginx 虚拟机配置详解

浏览器输入:http://www.nginx02.com/

Nginx 虚拟机配置详解

 > 成功!

 补充:如果配置不能正常访问,试参考 http://www.linuxidc.com/Linux/2017-06/144827.htm

二、基于 ip 的虚拟主机

1. 一块网卡绑定多个 ip
 [root@linuxidc.com nginx]# ifconfig eth0:1 10.219.24.27
 [root@linuxidc.com nginx]# ifconfig
 eth0 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02
 inet addr:10.219.24.26 Bcast:10.255.255.255 Mask:255.0.0.0
 …
 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:79:F4:02
 inet addr:10.219.24.27 Bcast:10.255.255.255 Mask:255.0.0.0
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 2. 配置通过 ip 区分的虚拟机
 [root@linuxidc.com nginx]# cat conf/nginx.conf
 user root root; #说明:这里的 user 根据 自己的 nginx.conf 文件所在的目录的属主属性而定
worker_processes 1;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;

server {
 listen 10.219.24.26:80;
 server_name www.nginx01.com;
 location / {
 root html;
 index index.html index.htm;
 }
 }

server {
 listen 10.219.24.27:80;
 server_name www.nginx01.com;
 location / {
 root /root/html;
 index index.html index.htm;
 }
 }
 }
 3. reopen nginx
 [root@linuxidc.com nginx]# ./sbin/nginx -s reopen

补充:
— 删除绑定的 vip
 ifconfig eth0:1 10.219.24.27 down

三、基于端口的虚拟主机

配置通过端口区分的虚拟机
[root@linuxidc.com nginx]# cat conf/nginx.conf
 user root root; #说明:这里的 user 根据 自己的 nginx.conf 文件所在的目录的属主属性而定
worker_processes 1;

events {
 worker_connections 1024;
 }

http {
 include mime.types;
 default_type application/octet-stream;

server {
 listen 80;
 server_name www.nginx01.com;
 location / {
 root html;
 index index.html index.htm;
 }
 }

server {
 listen 8080;
 server_name www.nginx01.com;
 location / {
 root /root/html;
 index index.html index.htm;
 }
 }
 }

CentOS 7 下 Nginx 服务器的安装配置  http://www.linuxidc.com/Linux/2017-04/142986.htm

CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向  http://www.linuxidc.com/Linux/2017-04/142642.htm

CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm

Linux 下安装 PHP 环境并配置 Nginx 支持 php-fpm 模块  http://www.linuxidc.com/Linux/2017-05/144333.htm

Nginx 服务的 SSL 认证和 htpasswd 认证  http://www.linuxidc.com/Linux/2017-04/142478.htm

Linux 中安装配置 Nginx 及参数详解  http://www.linuxidc.com/Linux/2017-05/143853.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置  http://www.linuxidc.com/Linux/2017-03/142168.htm

CentOS6.9 编译安装 Nginx1.4.7  http://www.linuxidc.com/Linux/2017-06/144473.htm

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-06/144826.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计6620字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中