Linux操做系統容許添加IP別名,IP別名就是在一塊物理網卡上綁定多個lP地址。這樣就可以在使用單一網卡的同一個服務器上運行多個基於IP的虛擬主機。html
需求:nginx
一臺nginx服務器綁定兩個ip:192.168.101.三、192.168.101.103apache
訪問不一樣的ip請求不一樣的html目錄,即:服務器
訪問http://192.168.101.3將訪問「html3」目錄下的html網頁app
訪問http://192.168.101.103將訪問「html103」目錄下的html網頁測試
html目錄建立spa
將原來nginx的html目錄拷貝兩個目錄 「html3」和「html103」,爲了方便測試須要修改每一個目錄下的index.html內容使之個性化。操作系統
cd /usr/local/nginxcode
cp -r html html3server
cp -r html html103
綁定多ip
一、將/etc/sysconfig/network-scripts/ifcfg-eth0文件複製一份,命名爲ifcfg-eth0:1
修改其中內容:
DEVICE=eth0:1
IPADDR=192.168.25.103
其餘項不用修改
二、重啓系統
配置虛擬主機
#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #配置虛擬主機192.168.101.3 server { #監聽的ip和端口,配置192.168.101.3:80 listen 80; #虛擬主機名稱這裏配置ip地址 server_name 192.168.101.3; #全部的請求都以/開始,全部的請求均可以匹配此location location / { #使用root指令指定虛擬主機目錄即網頁存放目錄 #好比訪問http://ip/test.html將找到/usr/local/html3/test.html #好比訪問http://ip/item/test.html將找到/usr/local/html3/item/test.html root /usr/local/nginx/html3; #指定歡迎頁面,按從左到右順序查找 index index.html index.htm; } } #配置虛擬主機192.168.101.103 server { listen 80; server_name 192.168.101.103; location / { root /usr/local/nginx/html103; index index.html index.htm; } } }
測試
訪問兩個IP