Nginx虚拟主机单网卡多IP配置

通过不同的IP区分不同的虚拟主机,此类虚拟主机对应的企业应用非常少见,一般不同业务需要使用多IP的常见都会在负载均衡器上进行VIP绑定,而不是在Web上通过绑定IP区分不同的虚拟机。
需求
一台Linux服务器绑定两个ip:192.168.66.100、192.168.66.101 访问不同的ip请求不同的html目录,即:
访问http://192.168.66.100将访问“html101”目录下的html网页
访问http://192.168.66.101将访问“html99”目录下的html网页
Linux绑定多IP
Linux操作系统允许绑定多IP。是在一块物理网卡上可以绑定多个lP 地址。这样就能够在使用单一网卡的同一个服务器上运行多个基于IP的虚拟主机。但是在绑定多IP时需要将动态的IP分配方式修改为静态的指定IP。
将动态IP修改为静态IP
[root@node1 ~]# cd /etc/sysconfig/network-scripts
[root@node1 network-scripts]# ls
ifcfg-ens33
[root@node1 network-scripts]# vim ifcfg-ens33
BOOTPROTO="static"
IPADDR0=192.168.66.100
IPADDR1=192.168.66.101
[root@node1 network-scripts]# service
network restart # centos6、7重启网卡
[root@node1 ~]# systemctl restart network  #centos7重启网卡
[root@node0 network-scripts]#reboot #各种发行版都是可以的
#CentOS8重启网卡 nmcli c reload ens33
 
修改Nginx的配置文件完成基于IP的虚拟主机配置
Nginx的配置文件nginx.conf
如上述配置文件所示,主要由6个部分组成:
main:用于进行nginx全局信息的配置
events:用于nginx工作模式的配置
http:用于进行http协议信息的一些配置
server:用于进行服务器访问信息的配置
location:用于进行访问路由的配置
修改配置nginx.conf
[root@node1 network-scripts]# vim
/usr/local/nginx/conf/nginx.conf
#一个Server就是一个虚拟主机
    server {
        listen       80;
 #为虚拟机指定IP或者是域名
        server_name 192.168.66.100;
 #主要配置路由访问信息
        location / {
    #用于指定访问根目录时,访问虚拟主机的web目录
            root   html100;
    #在不指定访问具体资源时,默认的展示资源的列表
            index  index.html index.htm;
       }  
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
            root   html;
       }
   }
    #一个Server就是一个虚拟主机
    server {
        listen       80;
    #为虚拟机指定IP或者是域名
    server_name 192.168.66.101;
    #主要配置路由访问信息
        location / {
    #用于指定访问根目录时,访问虚拟主机的web目录
            root   html101;
    #在不指定访问具体资源时,默认的展示资源的列表
            index  index.html index.htm;
       }  
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
            root   html;
       }
   }
} 
生效配置文件
./nginx -s reload
 
实时效果反馈
1. Linxu系统中修改_____配置文件设置双IP。
A /etc/sysconfig/network-scripts/ifcfg-ens33
B /etc/profile
C /etc/my.ini
D /etc/network.conf
2. Nginx技术中实现基于IP的虚拟主机,使用_____指令指定Ip。
A server_name
B server
C location
D root
Nginx虚拟主机基于域名的配置

需求
两个域名指向同一个nginx服务器,用户访问不同的域名时显示不同的内容。
域名规划:
1, www.tong.cn => 前台
2, www.tongxiao.cn =》 后台
修改windows的hosts文件配置域名与ip的映射
文件路径:C:\Windows\System32\drivers\etc\hosts
192.168.66.100 node1 www.tong.cn
www.tongxiao.cn 
修改nginx.conf配置文件
server {
    listen       80;
 #为虚拟机指定IP或者是域名
    server_name www.tong.cn;
 #主要配置路由访问信息
    location / {
    #用于指定访问根目录时,访问虚拟主机的web目录
        root   tong;
    #在不指定访问具体资源时,默认的展示资源的列表
        index  index.html index.htm;
   }
    error_page   500 502 503 504 /50x.html;
    location = /50x.html {
        root   html;
   }
}
#一个Server就是一个虚拟主机
server {
    listen       80;
#为虚拟机指定IP或者是域名
    server_name www.tongxiao.cn;
#主要配置路由访问信息
    location / {
    #用于指定访问根目录时,访问虚拟主机的web目录
        root   tongxiao;
    #在不指定访问具体资源时,默认的展示资源的列表
        index  index.html index.htm;
   }
    error_page   500 502 503 504 /50x.html;
    location = /50x.html {
        root   html;
   }
} 
准备需要的目录和html页面:
[root@node1 nginx]# cp -r html101/ bjsxt
[root@node1 nginx]# cp -r html101/ baizhan
[root@node1 nginx]# vim tong/index.html
<h1>Welcome to nginx tong!</h1>
<p><em>Thank you for using tong.</em></p>
[root@node1 nginx]# vim tongxiao/index.html
<h1>Welcome to tongxiao!</h1>
<p><em>Thank you for using tongxiao.</em></p> 
重启Nginx服务
[root@node1 html]# ./nginx -s reload
 
实时效果反馈
1. windows操作系统中在_____设置ip和域名映射关系
A C:\Windows\System32\drivers\etc\hosts
B C:\Windows\etc\hosts
C C:\Windows\drivers\etc\hosts
D C:\Windows\System32\drivers\hosts
2. Nginx技术中实现基于域名的虚拟主机,使用_____指令指定域 名。
A root
B server
C location
D server_name



















