nginx搭建基于IP和域名的虚拟主机

本机IP地址eth0: 192.168.4.44
1 添加两个IP地址绑定到eth0口

[root@web44 ~]# cd /etc/sysconfig/network-scripts/
[root@web44 network-scripts]# cp ifcfg-eth0{,:1}
[root@web44 network-scripts]# cp ifcfg-eth0{,:2}
[root@web44 network-scripts]# ls
ifcfg-eth0    ifdown-ipv6      ifup-aliases  ifup-ppp
ifcfg-eth0:1  ifdown-isdn      ifup-bnep     ifup-routes
ifcfg-eth0:2
[root@web44 network-scripts]# vim ifcfg-eth0:1
NAME=eth0:1     //相对于eth0的配置改动这三行即可
DEVICE=eth0:1
IPADDR=192.168.4.5
NAME=eth0:1

[root@web44 network-scripts]# vim ifcfg-eth0:2
DEVICE=eth0:2
IPADDR=192.168.4.8
NAME=eth0:2

重启网络服务
[root@web44 network-scripts]# systemctl restart network

查看eth0口的ip
[root@web44 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.4.44  netmask 255.255.255.0  broadcast 192.168.4.255
        inet6 fe80::e439:8b58:a63e:3cb3  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:cf:68:69  txqueuelen 1000  (Ethernet)
        RX packets 1863559  bytes 304008397 (289.9 MiB)
        RX errors 0  dropped 53  overruns 0  frame 0
        TX packets 288327  bytes 975462228 (930.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.4.5  netmask 255.255.255.0  broadcast 192.168.4.255
        ether 52:54:00:cf:68:69  txqueuelen 1000  (Ethernet)

eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.4.8  netmask 255.255.255.0  broadcast 192.168.4.255
        ether 52:54:00:cf:68:69  txqueuelen 1000  (Ethernet)
        
检测在客户机33主机上能不能ping通
[root@web33 ~]# ping 192.168.4.5
PING 192.168.4.5 (192.168.4.5) 56(84) bytes of data.
64 bytes from 192.168.4.5: icmp_seq=1 ttl=64 time=0.540 ms
[root@web33 ~]# ping 192.168.4.8
PING 192.168.4.8 (192.168.4.8) 56(84) bytes of data.
64 bytes from 192.168.4.8: icmp_seq=1 ttl=64 time=0.452 ms

2 主机44修改nginx配置文件

[root@web44 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen       192.168.4.5:80;        //基于IP地址的虚拟主机
        server_name  web1.example.com;         //基于域名的虚拟主机
        location / {
            root   html;   //网页根目录的默认路径
            index  index.php index.html index.htm;
        }
}

 server {
        listen      192.168.4.8:80;     
        server_name  web2.example.com;

        location / {
            root   www;         //指定网页根目录
            index  index.html index.htm;
        }
}

重新加载配置文件
[root@web44 ~]# nginx -s reload
查看nginx程序的监听端口
[root@web44 ~]# ss -antulp | grep nginx
tcp    LISTEN     0      128    192.168.4.8:80                    *:*                   users:(("nginx",pid=3169,fd=7),("nginx",pid=3168,fd=7))
tcp    LISTEN     0      128    192.168.4.5:80                    *:*                   users:(("nginx",pid=3169,fd=6),("nginx",pid=3168,fd=6))

3 准备网页测试文件

[root@web44 ~]# echo "我是192.168.4.5" > /usr/local/nginx/html/index.html
[root@web44 network-scripts]# mkdir /usr/local/nginx/www
[root@web44 ~]# echo "我是192.168.4.8" > /usr/local/nginx/www/index.html

4 客户机192.168.4.33 编写/etc/hosts文件用于解析域名

[root@web33 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.5  web1.example.com   
192.168.4.8 web2.example.com

5 客户端测试:

[root@web33 ~]# curl web1.example.com
我是192.168.4.5
[root@web33 ~]# curl web2.example.com
我是192.168.4.8

猜你喜欢

转载自blog.csdn.net/weixin_42104231/article/details/84929543