高性能 WEB 服务器 Nginx:多虚拟主机实现
|
admin
2025年6月29日 18:40
本文热度 1054
|
2 Nginx 配置
2.4 多虚拟主机实现
多虚拟主机是指在一台 Nginx 服务器上配置多个网站
在 Nginx 中,多虚拟主机有三种实现方式:
2.4.1 基于域名实现多虚拟主机
在 Nginx 中配置多个 server 段,每个 server 中设置一个虚拟主机配置,客户端访问服务器时,会根据客户端请求头中的 Host 字段值来匹配 server 段中的配置,从而访问不同的网站
[root@ubuntu ~]# cd /etc/nginx/sites-enabled/
#设置两个域名配置文件[root@ubuntu sites-enabled]# cat www.m99-josedu.comserver { listen 80; server_name www.m99-josedu.com; root /var/www/html/www.m99-josedu.com;}
[root@ubuntu sites-enabled]# cat www.m99-josedu.netserver { listen 80; server_name www.m99-josedu.net; root /var/www/html/www.m99-josedu.net;}
#修改默认页面,创建网站目录和首页[root@ubuntu ~]# mv /var/www/html/index.nginx-debian.html /tmp/[root@ubuntu ~]# echo "welcome to nginx" > /var/www/html/index.html[root@ubuntu ~]# mkdir /var/www/html/www.m99-josedu.{com,net}[root@ubuntu ~]# echo "this page from com" > /var/www/html/www.m99-josedu.com/index.html[root@ubuntu ~]# echo "this page from net" > /var/www/html/www.m99-josedu.net/index.html[root@ubuntu ~]# tree /var/www/html//var/www/html/├── index.html├── www.m99-josedu.com│ └── index.html└── www.m99-josedu.net └── index.html2 directories, 3 files
#测试配置文件并重新加载服务[root@ubuntu ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@ubuntu ~]# systemctl reload nginx.service
#客户端配置域名解析并测试[root@ubuntu ~]# cat /etc/hosts10.0.0.206 www.m99-josedu.com www.m99-josedu.net[root@ubuntu ~]# curl www.m99-josedu.comthis page from com[root@ubuntu ~]# curl www.m99-josedu.netthis page from net
#当前没有设置 index,使用默认index配置[root@ubuntu ~]# cat /etc/nginx/sites-enabled/www.m99-josedu.comserver { listen 80; server_name www.m99-josedu.com; root /var/www/html/www.m99-josedu.com;}
[root@ubuntu ~]# ls -l /var/www/html/www.m99-josedu.com/total 12-rw-r--r-- 1 root root 4 Jan 23 17:49 a.txt-rw-r--r-- 1 root root 8 Jan 23 17:49 default.htm-rw-r--r-- 1 root root 19 Jan 23 09:05 index.html
#测试,默认 index.html 生效[root@ubuntu ~]# curl www.m99-josedu.comthis page from com
#修改配置[root@ubuntu ~]# cat /etc/nginx/sites-enabled/www.m99-josedu.comserver { listen 80; index index.html default.htm a.txt; server_name www.m99-josedu.com; root /var/www/html/www.m99-josedu.com;}
#重载生效[root@ubuntu ~]# systemctl reload nginx.service
#客户端测试 默认页面是 index.html[root@ubuntu ~]# curl www.m99-josedu.comthis page from com
#服务器上删除 index.html[root@ubuntu ~]# rm -f /var/www/html/www.m99-josedu.com/index.html[root@ubuntu ~]# ls /var/www/html/www.m99-josedu.com/a.txt default.htm
#客户端测试 默认页面是 default.htm[root@ubuntu ~]# curl www.m99-josedu.comdefault
#服务器上删除 default.htm[root@ubuntu ~]# rm -f /var/www/html/www.m99-josedu.com/default.htm[root@ubuntu ~]# ls /var/www/html/www.m99-josedu.com/a.txt
#客户端测试 默认页面是 a.txt[root@ubuntu ~]# curl www.m99-josedu.comaaa
#服务器上删除 a.txt[root@ubuntu ~]# rm -f /var/www/html/www.m99-josedu.com/a.txt[root@ubuntu ~]# ls /var/www/html/www.m99-josedu.com/
#客户端测试 报错[root@ubuntu ~]# curl www.m99-josedu.com<html><head><title>403 Forbidden</title></head><body><center><h1>403 Forbidden</h1></center><hr><center>nginx</center></body></html>
2.4.3 设置服务器的默认虚拟主机
用 http 协议访问服务器IP或没有在服务器上配置的域名时,会被默认虚拟主机命中
[root@ubuntu ~]welcome to nginx
[root@ubuntu ~]include /etc/nginx/sites-enabled/*;
[root@ubuntu ~]total 8.0Klrwxrwxrwx 1 root root 34 Jan 21 00:36 default -> /etc/nginx/sitesavailable/default-rw-r--r-- 1 root root 100 Jan 23 09:04 www.m99-josedu.com-rw-r--r-- 1 root root 100 Jan 23 09:06 www.m99-josedu.net
[root@ubuntu ~]server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; }}
[root@ubuntu ~]10.0.0.206 www.m99-josedu.com www.m99-josedu.net abc.m99-josedu.org def.m99-josedu.org
[root@ubuntu ~]welcome to nginx[root@ubuntu ~]welcome to nginx
#修改服务端配置,新增域名解析[root@ubuntu ~]# cat /etc/nginx/sites-enabled/abc.m99-josedu.orgserver { listen 80; server_name abc.m99-josedu.org; root /var/www/html/abc.m99-josedu.org;}
[root@ubuntu ~]# mkdir /var/www/html/abc.m99-josedu.org[root@ubuntu ~]# echo "this page from abc.m99-josedu.org" > /var/www/html/abc.m99-josedu.org/index.html[root@ubuntu ~]# systemctl reload nginx.service
#客户端测试-命中域名配置[root@ubuntu ~]# curl abc.m99-josedu.orgthis page from abc.m99-josedu.org
#客户端测试-命中默认配置[root@ubuntu ~]# curl def.m99-josedu.orgwelcome to nginx
#去掉默认配置中的 default_server[root@ubuntu ~]# cat /etc/nginx/sites-enabled/defaultserver { #listen 80 default_server; #listen [::]:80 default_server; listen 80; ......
#重载服务并测试[root@ubuntu ~]# systemctl reload nginx.service[root@ubuntu ~]# curl abc.m99-josedu.orgthis page from abc.m99-josedu.org
#此处被 abc 的配置命中[root@ubuntu ~]# curl def.m99-josedu.orgthis page from abc.m99-josedu.org
#IP也被 abc 配置命中[root@ubuntu ~]# curl http://10.0.0.206this page from abc.m99-josedu.org
#在没有定义 default_server 的情况下,默认会被第一个配置文件命中#主配置文件中会按此顺序引用文件[root@ubuntu ~]# ls /etc/nginx/sites-enabled/abc.m99-josedu.org default www.m99-josedu.com www.m99-josedu.net[root@ubuntu ~]# ls -l /etc/nginx/sites-enabled/total 12-rw-r--r-- 1 root root 100 Jan 23 10:03 abc.m99-josedu.orglrwxrwxrwx 1 root root 34 Jan 21 00:36 default -> /etc/nginx/sitesavailable/default-rw-r--r-- 1 root root 100 Jan 23 09:50 www.m99-josedu.com-rw-r--r-- 1 root root 100 Jan 23 09:06 www.m99-josedu.net
#修改文件名[root@ubuntu ~]# mv /etc/nginx/sites-enabled/{www.m99-josedu.com,aaa.m99-josedu.com}
#重载服务[root@ubuntu ~]# systemctl reload nginx.service[root@ubuntu ~]# ls /etc/nginx/sites-enabled/aaa.m99-josedu.com abc.m99-josedu.org default www.m99-josedu.net
#再次测试,默认被最先引用的配置文件命中[root@ubuntu ~]# curl def.m99-josedu.orgthis page from com[root@ubuntu ~]# curl http://10.0.0.206this page from com
#改回文件名,并设置 default_server[root@ubuntu ~]# mv /etc/nginx/sites-enabled/{aaa.m99-josedu.com,www.m99-josedu.com}[root@ubuntu ~]# ls /etc/nginx/sites-enabled/abc.m99-josedu.org default www.m99-josedu.com www.m99-josedu.net
#www.m99-josedu.net 文件在引用时被放在最后,但设置了 default_server[root@ubuntu ~]# cat /etc/nginx/sites-enabled/www.m99-josedu.netserver { listen 80 default_server; server_name www.m99-josedu.net; root /var/www/html/www.m99-josedu.net;}[root@ubuntu ~]# systemctl reload nginx.service
#客户端测试[root@ubuntu ~]# curl http://10.0.0.206this page from net[root@ubuntu ~]# curl def.m99-josedu.orgthis page from net
[root@ubuntu ~][root@ubuntu ~][root@ubuntu ~][root@ubuntu ~]2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:11:98:d9 brd ff:ff:ff:ff:ff:ff altname enp2s1 inet 10.0.0.206/24 metric 100 brd 10.0.0.255 scope global dynamic ens33 valid_lft 1118sec preferred_lft 1118sec inet 10.0.0.216/24 scope global secondary ens33 valid_lft forever preferred_lft forever inet 10.0.0.226/24 scope global secondary ens33 valid_lft forever preferred_lft forever inet 10.0.0.236/24 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe11:98d9/64 scope link valid_lft forever preferred_lft forever
[root@ubuntu ~]server { listen 10.0.0.206:80; listen 10.0.0.216:80; server_name abc.m99-josedu.org; root /var/www/html/abc.m99-josedu.org;}
[root@ubuntu ~]server { listen 10.0.0.226:80; server_name www.m99-josedu.com; root /var/www/html/www.m99-josedu.com;}
[root@ubuntu ~]abc.m99-josedu.org default www.m99-josedu.com www.m99-josedu.net[root@ubuntu ~]
[root@ubuntu ~]10.0.0.206 www.m99-josedu.net10.0.0.216 abc.m99-josedu.org10.0.0.226 www.m99-josedu.com[root@ubuntu ~]this page from com[root@ubuntu ~]this page from abc.m99-josedu.org[root@ubuntu ~]this page from com
[root@ubuntu ~]this page from abc.m99-josedu.org[root@ubuntu ~]this page from abc.m99-josedu.org[root@ubuntu ~]this page from com[root@ubuntu ~]welcome to nginx
#服务器上所有IP 的81,82端口都会被此规则匹配[root@ubuntu ~]# cat /etc/nginx/sites-enabled/abc.m99-josedu.orgserver { listen 81; listen 82; server_name abc.m99-josedu.org; root /var/www/html/abc.m99-josedu.org;}
#所有88端口的访问都会被此规则匹配[root@ubuntu ~]# cat /etc/nginx/sites-enabled/www.m99-josedu.comserver { listen 88; server_name www.m99-josedu.com; root /var/www/html/www.m99-josedu.com;}[root@ubuntu ~]# systemctl reload nginx.service
#客户端测试[root@ubuntu ~]# curl 10.0.0.206welcome to nginx[root@ubuntu ~]# curl 10.0.0.206:81this page from abc.m99-josedu.org[root@ubuntu ~]# curl 10.0.0.206:82this page from abc.m99-josedu.org[root@ubuntu ~]# curl 10.0.0.206:88this page from com
#用默认端口,被默认规则匹配[root@ubuntu ~]# curl abc.m99-josedu.orgwelcome to nginx
#指定域名和端口[root@ubuntu ~]# curl abc.m99-josedu.org:81this page from abc.m99-josedu.org[root@ubuntu ~]# curl abc.m99-josedu.org:82this page from abc.m99-josedu.org
#指定域名和端口,被服务器上默认 88 匹配[root@ubuntu ~]# curl abc.m99-josedu.org:88this page from com
— END —
阅读原文:原文链接
该文章在 2025/7/1 22:59:56 编辑过