Nginx 虛擬主機
定義:爲不同的域名/IP/端口不同的接口服務。利用虛擬主機,不用爲每個要運行的網站提供一臺單獨的Nginx服務器或單獨運行一組Nginx進程。虛擬主機提供了在同一臺服務器、同一組Nginx進程上運行多個網站的功能
-
基於域名的虛擬主機
沒有DNS服務,所以本機的Windows需要配置hosts
路徑:C:\Windows\System32\drivers\etc\hosts
192.168.249.137 water.com water1.com aa.com bb.com cc.com
nginx.conf
http{
include /usr/local/nginx/conf/aa.conf;
include /usr/local/nginx/conf/bb.conf;
include /usr/local/nginx/conf/cc.conf;
…
}
aa.conf
server {
listen 80 default_server;
server_name aa.com;
location / {
root /etc/nginx/html;
index aa.html;
}
bb.conf
server {
listen 80;
server_name bb.com;
location / {
root /etc/nginx/html;
index bb.html;
}
cc.conf
server {
listen 80;
server_name cc.com;
location / {
root /etc/nginx/html;
index cc.html;
}
訪問結果:
http://aa.com
http://bb.com
http://cc.com
2.基於端口的虛擬主機
對原來的配置文件稍做修改
aa.conf
listen 8080 default_server;
server_name water.com;
bb.conf
listen 8081 ;
server_name water.com;
cc.conf
listen 8082 ;
server_name water.com;
3.基於IP的虛擬主機
aa.conf
listen 192.168.249.137:8080 default_server;
server_name water.com;
bb.conf
listen 192.168.249.136:8081;
server_name water.com;
cc.conf
listen 192.168.249.135:8082;
server_name water.com;