1. 程式人生 > >nginx搭建基於IP和域名的虛擬主機

nginx搭建基於IP和域名的虛擬主機

本機IP地址eth0: 192.168.4.44
1 新增兩個IP地址繫結到eth0口

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-eth0{,:1}
[[email protected] network-scripts]# cp ifcfg-eth0{,:2}
[[email protected] network-scripts]# ls
ifcfg-eth0    ifdown-ipv6      ifup-aliases  ifup-ppp
ifcfg-eth0:1  ifdown-isdn      ifup-bnep     ifup-routes
ifcfg-eth0:2
[
[email protected]
network-scripts]# vim ifcfg-eth0:1 NAME=eth0:1 //相對於eth0的配置改動這三行即可 DEVICE=eth0:1 IPADDR=192.168.4.5 NAME=eth0:1 [[email protected] network-scripts]# vim ifcfg-eth0:2 DEVICE=eth0:2 IPADDR=192.168.4.8 NAME=eth0:2 重啟網路服務 [[email protected] network-scripts]# systemctl restart network 檢視eth0口的ip [
[email protected]
~]# 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通 [
[email protected]
~]# 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 [[email protected] ~]# 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配置檔案

[[email protected] ~]# 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;
        }
}

重新載入配置檔案
[[email protected] ~]# nginx -s reload
檢視nginx程式的監聽埠
[[email protected] ~]# 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 準備網頁測試檔案

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

4 客戶機192.168.4.33 編寫/etc/hosts檔案用於解析域名

[[email protected] ~]# 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 客戶端測試:

[[email protected] ~]# curl web1.example.com
我是192.168.4.5
[[email protected] ~]# curl web2.example.com
我是192.168.4.8