关于虚拟主机server 的 listen 、server_name 配置:
- listen 对应nginx虚拟主机对外提供服务的端口
- server_name 可以理解成虚拟主机的名字,这个值会和http请求里的Host header做匹配
所以 1、在同一个nginx.conf配置文件里,只要 listen 、server_name组合起来唯一就没有问题;
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.nginxo.com;
location / {
root html;
index index2.html index2.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2、server_name 这一行可以配置多个,中间用空格隔开,例如:
server {
listen 80;
server_name www.nginxo.com www.nginxp.com;
location / {
root html;
index index1.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3、server_name 也可以配置通配符
server {
listen 80;
server_name *.nginxo.com;
location / {
root html;
index index1.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4、server_name 也可以配置正则 用 ~ 符号表示正则匹配
server {
listen 80;
server_name ~^[0-9]+\.nginxo\.com$;
location / {
root html;
index index1.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server_name 匹配规则
从上到下,匹配到后不再继续匹配
|