一、创建不同的站点目录
mkdir /www
cd /www
mkdir video music
然后在为不同的站点设置不同的主页:
echo "This is video site">./video/index.html
echo "This is music site">./music/index.html
二、配置 nginx.conf
2.1 不同端口号的虚拟主机
8080 => music
8081 => video
server {
listen 8080;
server_name localhost;
location / {
root /www/music;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8081;
server_name localhost;
location / {
root /www/video;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
需要重启nginx
systemctl reload nginx.service
温馨提示
在配置多个站点时候 listen + server_name
必须唯一,否则无法启动。
我们可以为一个站点配置多个端口或者域名,之间用空格分隔。
匹配规则是先匹配先前面的站点,一旦匹配成功就不会再匹配后面配置的站点了。
域名匹配支持:完整匹配,通配符匹配,通配符结束匹配,正则匹配。
完整匹配
server_name www.example.com;
通配符匹配
server_name *.example.com;
通配符结束匹配
server_name example.*;
正则匹配
server_name ~^[0-9]+\.example\.com$;
# 表示匹配数字开的 .example.com 结尾的