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;