nginx配置基于IP的虚拟主机

Nginx支持多种类型的虚拟主机:基于IP的虚拟主机,基于域名的虚拟主机,基于端口的虚拟主机

增加两个IP别名

ifconfig ens33:1 192.168.86.135 broadcast 192.168.86.255 netmask 255.255.255.0 up
route add -host 192.168.86.135 dev ens33:1
ifconfig ens33:2 192.168.86.136 broadcast 192.168.86.255 netmask 255.255.255.0 up
route add -host 192.168.86.136 dev ens33:2

上面配置在服务器重启后会消失,可以将上面四条命令添加到/etc/rc.local文件中,让系统开机自动运行。
以下是我虚拟机ifconfig的显示:

[root@localhost conf]# ifconfig 
docker0: flags=4099  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:12ff:fe51:e240  prefixlen 64  scopeid 0x20
        ether 02:42:12:51:e2:40  txqueuelen 0  (Ethernet)
        RX packets 124272  bytes 21817469 (20.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 160234  bytes 85904535 (81.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163  mtu 1500
        inet 192.168.86.134  netmask 255.255.255.0  broadcast 192.168.86.255
        inet6 fe80::c813:ac4c:c09e:e9ad  prefixlen 64  scopeid 0x20
        ether 00:0c:29:19:88:40  txqueuelen 1000  (Ethernet)
        RX packets 646648  bytes 542092195 (516.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 299813  bytes 49072436 (46.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163  mtu 1500
        inet 192.168.86.135  netmask 255.255.255.0  broadcast 192.168.86.255
        ether 00:0c:29:19:88:40  txqueuelen 1000  (Ethernet)

ens33:2: flags=4163  mtu 1500
        inet 192.168.86.136  netmask 255.255.255.0  broadcast 192.168.86.255
        ether 00:0c:29:19:88:40  txqueuelen 1000  (Ethernet)

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 18546  bytes 6420545 (6.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18546  bytes 6420545 (6.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:df:d8:67  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

路由表信息,可以使用netstat -rn或者Ip route(这种方式不利于查看)

[root@localhost conf]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.86.2    0.0.0.0         UG    100    0        0 ens33
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.86.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.86.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.86.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.86.135  0.0.0.0         255.255.255.255 UH    0      0        0 ens33
192.168.86.136  0.0.0.0         255.255.255.255 UH    0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

在nginx的配置文件中,配置三个基于IP的虚拟主机,如下所示:

		# 第一个虚拟主机
        server {
                listen 192.168.86.134:80;
                server_name 192.168.86.134;
                access_log logs/134.access.log combined;
                location / {
                        root /root/hbk/server134;
                        index  index.html;
                }
        }

        # 第二个虚拟主机
        server {
                listen 192.168.86.135:80;
                server_name 192.168.86.135;
                access_log logs/135.access.log combined;
                location / {
                        root /root/hbk/server135;
                        index  index.html;
                }
        }
        # 第三个虚拟主机
        server {
                listen 192.168.86.136:80;
                server_name 192.168.86.136;
                access_log logs/136.access.log combined;
                location / {
                        root /root/hbk/server136;
                        index  index.html;
                }
        }

在/root/hbk目录下建立对应目录

[root@localhost hbk]#mkdir server13{4,5,6}
[root@localhost hbk]# cd server134/
[root@localhost server134]# echo "server134 huangbaokang" > index.html
[root@localhost server134]# echo "server135 huangbaokang" > ../server135/index.html
[root@localhost server134]# echo "server136 huangbaokang" > ../server136/index.html

重启nginx
浏览器访问测试:
nginx配置基于IP的虚拟主机_第1张图片
nginx配置基于IP的虚拟主机_第2张图片.
nginx配置基于IP的虚拟主机_第3张图片

你可能感兴趣的:(nginx)