一、虛擬主機介紹javascript
現象css
一個web服務器軟件默認狀況下只能發佈一個web,由於web分享出去須要三個條件IP、Port、Domainhtml
介紹html5
虛擬主機:就是把一臺服務器劃分紅多個虛擬的服務器,每個虛擬主機均可以有獨立的域名和獨立的目錄java
如何能發佈多個web呢?node
二、基於IP虛擬主機python
ifconfig ens33:1 192.168.2.52/24 up
ifconfig ens33:1 down
同時發佈兩個網站:jquery
DocumentRoot: /usr/local/nginx/html/web2linux
server { listen 192.168.2.42:80; server_name localhost; charset utf-8; location / { root html/web01; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.2.52:80; server_name localhost; charset utf-8; location / { root html/web02; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
缺點:須要多個ip,若是是公網IP,每一個IP都須要付費android
三、基於端口的虛擬主機
server { listen 80; server_name localhost; charset utf-8; location / { root html/web01; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name localhost; charset utf-8; location / { root html/web02; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
缺點:只須要一個IP,可是端口你是沒法告訴公網用戶,沒法適用於公網客戶,適合內部用戶
四、基於域名的虛擬主機
修改:/etc/hosts/
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.42 www.abc.com 192.168.2.42 www.def.com
修改:/nginx.conf/
server { listen 80; server_name www.abc.com; charset utf-8; location / { root html/web01; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.def.com; charset utf-8; location / { root html/web02; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
測試:
[root@Web01 nginx]# elinks http://www.abc.com -dump i am web01 [root@Web01 nginx]# elinks http://www.def.com -dump i am web02