一、虛擬主機概念
虛擬主機在web服務器裏就是一個獨立的網站站點,這個站點對應獨立的域名(也多是IP或端口),具備獨立的程序及資源目錄,能夠獨立的對外提供服務供用戶訪問。一個web服務裏能夠同時支持多個虛擬主機站點html
二、虛擬主機類型
常見的虛擬主機類型有以下幾種。nginx
(1)基於域名的虛擬主機web
所謂基於域名的虛擬主機,意思就是經過不一樣的域名區分不一樣的虛擬主機,基於域名的虛擬主機是企業應用最廣的虛擬主機類型,幾乎全部對外提供服務的網站使用的都是基於域名的虛擬主機,例如:www.etiantian.orgvim
(2)基於端口的虛擬主機瀏覽器
同理,所謂基於端口的虛擬主機,意思就是經過不一樣的端口來區分不一樣的虛擬主機,此類虛擬主機對應的企業應用主要爲公司內部的網站,例如:一些不但願直接對外提供用戶訪問的網站後臺等,訪問基於端口的虛擬主機,地址裏要帶有端口,例如:http://www.etiantian.org:9000服務器
(3)基於IP的虛擬主機app
同理,所謂基於IP的虛擬主機,意思就是經過不一樣的IP區分不一樣的虛擬主機,此類虛擬主機對應的企業應用很是少見。通常不一樣的業務須要使用多IP的場景都會在負載均衡器上進行VIP綁定,而不是在Web上綁定I來區分不一樣的虛擬機。負載均衡
三種虛擬主機類型都可獨立使用,也能夠混合使用。curl
一、配置基於域名的虛擬主機tcp
[root@nginx ~]# cd /application/nginx/conf/ [root@nginx conf]# diff nginx.conf.default nginx.conf #初始時這兩個配置文件是一致的; [root@nginx conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf #過濾包含#號和空行,生成新文件nginx.conf 或者直接建立新的配置文件nginx.conf,而後編輯 [root@nginx conf]# vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.dmtest1.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 這樣,一個基於域名的站點就建立好了。
二、建立域名對應的站點目錄及文件
[root@nginx conf]# mkdir ../html/www -p #建立www目錄; [root@nginx conf]# echo "http://www.dmtest1.com.org" >../html/www/index.html #生成一個默認首頁文件; [root@nginx conf]# cat ../html/www/index.html #查看生成的默認首頁文件; http://www.dmtest1.com.org
三、檢查語法並重載nginx
[root@nginx conf]# ../sbin/nginx -t #檢查nginx配置文件語法是否正確; nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@nginx conf]# systemctl reload nginx #平滑重啓nginx; [root@nginx conf]# ps -ef |grep nginx #查看nginx啓動進程; root 1100 1 0 19:01 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx www 1428 1100 0 19:24 ? 00:00:00 nginx: worker process root 1430 1293 0 19:24 pts/0 00:00:00 grep --color=auto nginx [root@nginx conf]# netstat -lntp | grep 80 #查看是否監聽在80 端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1100/nginx: master ======================================================================================================================== 測試 Linux測試: [root@nginx conf]# echo "192.168.200.102 www.dmtest1.com" >>/etc/hosts [root@nginx conf]# tail -1 /etc/hosts 192.168.200.102 www.dmtest1.com [root@nginx conf]# curl www.dmtest1.com http://www.dmtest1.com.org Windows測試: Windows下須要配置hosts解析,hosts文件在C:\Windows\System32\drivers\etc\目錄下 在hosts文件中的添加方法和Linux同樣。
四、配置多個基於域名的虛擬主機
多個域名虛擬主機的配置的區別就是server_name和root參數的配置不一樣,下面是基於三個域名的虛擬主機配置:
[root@nginx conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.dmtest1.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.dmtest2.com; location / { root html/dmtest2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.dmtest3.com; location / { root html/dmtest3; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
建立虛擬主機站點對應的目錄和文件
[root@nginx conf]# mkdir ../html/dmtest2 ../html/dmtest3 -p [root@nginx conf]# echo "http://dmtest2.com" >../html/dmtest2/index.html [root@nginx conf]# echo "http://dmtest3.com" >../html/dmtest3/index.html [root@nginx conf]# cat ../html/dmtest2/index.html http://dmtest2.com [root@nginx conf]# cat ../html/dmtest3/index.html http://dmtest3.com
目錄結構以下:
root@nginx conf]# tree ../html/ ../html/ ├── 50x.html ├── dmtest2 #dmtest2站點目錄; │ └── index.html ├── dmtest3 #dmtest3站點目錄; │ └── index.html ├── index.html └── www #www站點目錄 └── index.html 3 directories, 5 files
檢查並重載nginx配置文件
root@nginx conf]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@nginx conf]# systemctl reload nginx
測試
[root@nginx conf]# echo "192.168.200.102 www.dmtest2.com" >>/etc/hosts [root@nginx conf]# echo "192.168.200.102 www.dmtest3.com" >>/etc/hosts [root@nginx conf]# curl www.dmtest2.com http://dmtest2.com [root@nginx conf]# curl www.dmtest3.com http://dmtest3.com [root@nginx conf]# curl www.dmtest1.com http://www.dmtest1.com.org
若是要配置基於端口的虛擬主機,就須要爲每一個虛擬主機配置不一樣的端口。編輯nginx.conf主配置文件,把每一個虛擬主機的「listen 80」,這個配置行的80端口修改掉,server_name域名位置能夠不用改變。
root@nginx conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.dmtest1.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 88; server_name www.dmtest2.com; location / { root html/dmtest2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 888; server_name www.dmtest3.com; location / { root html/dmtest3; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
檢查語法並重載nginx
[root@nginx conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@nginx conf]# systemctl reload nginx [root@nginx conf]# netstat -lntup|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1100/nginx: master tcp 0 0 0.0.0.0:888 0.0.0.0:* LISTEN 1100/nginx: master tcp 0 0 0.0.0.0:88 0.0.0.0:* LISTEN 1100/nginx: master
防火牆開啓88和888端口
[root@nginx conf]# firewall-cmd --zone=public --add-port=88/tcp --permanent success [root@nginx conf]# firewall-cmd --zone=public --add-port=888/tcp --permanent success [root@nginx conf]# firewall-cmd --reload success [root@nginx conf]# firewall-cmd --list-port 80/tcp 88/tcp 888/tcp
測試
[root@nginx conf]# curl 192.168.200.102:88 http://dmtest2.com [root@nginx conf]# curl 192.168.200.102:888 http://dmtest3.com [root@nginx conf]# curl 192.168.200.102:80 http://www.dmtest1.com.org
在服務器網卡上增長多個IP
[root@nginx conf]# ip addr add 192.168.200.103/24 dev ens34 [root@nginx conf]# ip addr add 192.168.200.104/24 dev ens34 [root@nginx conf]# ip add | grep 192.168.200 #查看添加的IP; inet 192.168.200.102/24 brd 192.168.200.255 scope global noprefixroute ens34 inet 192.168.200.103/24 scope global secondary ens34 inet 192.168.200.104/24 scope global secondary ens34 [root@nginx conf]# ping -c 3 192.168.200.104 #測試添加到的IP是否正常; PING 192.168.200.104 (192.168.200.104) 56(84) bytes of data. 64 bytes from 192.168.200.104: icmp_seq=1 ttl=64 time=0.024 ms 64 bytes from 192.168.200.104: icmp_seq=2 ttl=64 time=0.035 ms 64 bytes from 192.168.200.104: icmp_seq=3 ttl=64 time=0.032 ms --- 192.168.200.104 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 0.024/0.030/0.035/0.006 ms [root@nginx conf]# ping -c 3 192.168.200.103 #測試添加到的IP是否正常; PING 192.168.200.103 (192.168.200.103) 56(84) bytes of data. 64 bytes from 192.168.200.103: icmp_seq=1 ttl=64 time=0.023 ms 64 bytes from 192.168.200.103: icmp_seq=2 ttl=64 time=0.034 ms 64 bytes from 192.168.200.103: icmp_seq=3 ttl=64 time=0.033 ms --- 192.168.200.103 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.023/0.030/0.034/0.005 ms
修改nginx配置文件以下:
root@nginx conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 192.168.200.102:80; server_name www.dmtest1.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.200.103:88; server_name www.dmtest2.com; location / { root html/dmtest2; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.200.104:888; server_name www.dmtest3.com; location / { root html/dmtest3; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } #這是一個端口和IP混合的虛擬主機配置,能夠根據須要自行修改,使其僅僅基於IP,即把每一個虛擬主機的server_name字段都換成IP地址。
檢查配置文件並重載
[root@nginx conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful [root@nginx conf]# systemctl reload nginx
測試
root@nginx conf]# curl 192.168.200.102:80 http://www.dmtest1.com.org [root@nginx conf]# curl 192.168.200.103:88 http://dmtest2.com [root@nginx conf]# curl 192.168.200.104:888 http://dmtest3.com [root@nginx conf]# curl www.dmtest1.com:80 http://www.dmtest1.com.org [root@nginx conf]# curl www.dmtest2.com:88 http://dmtest2.com [root@nginx conf]# curl www.dmtest3.com:888 http://dmtest3.com
5、nginx虛擬主機配置步驟
總結以下:
1)增長一個完整的server標籤段到結尾處。注意,要放在http的結束大括號前也就是將server標籤段放入http標籤。
2)更改server_name及對應網頁的root根目錄,若是須要其餘參數,能夠增長或修改。
3)建立server_name域名對應網頁的根目錄,而且創建測試文件,若是沒有index首頁,訪問會出現403錯誤。
4)檢查 Nginx配置文件語法,平滑重啓Nginx服務,快速檢查啓動結果。
5)在客戶端對server_name處配置的域名作host解析或DNS配置,並檢查(ping域名看返回的IP是否正確)。
6)在瀏覽器中輸入地址訪問,或者在Linux客戶端作hosts解析,用wget或curl接地址訪問。
Nginx虛擬主機的官方幫助網址爲http://ngInx.org/en/docs/http/request_processing.html。