Nginx server_name

匹配规则

完全匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
http {  
server {
listen 80;
server_name test1.com;

location / {
return 500;
}
}
server {
listen 80;
server_name test2.com;

location / {
return 508;
}
}
}
  • 测试命令

curl -i “test1.com/“ -> 查询server_name成功,默认匹配到第1个server,返回500

curl -i “test2.com/“ -> 查询server_name成功,默认匹配到第2个server,返回508

curl -i “test3.com/“ -> 查询server_name失败,默认匹配到第1个server,返回500

禁止非法域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {  
server {
listen 80;
server_name test1.com;
location / {
return 500;
}
}

server {
listen 80 default_server;
server_name _;
return 403;
}
}
  • 测试命令

curl -i “test1.com/“ -> 查询server_name成功,返回500

curl -i “test2.com/“ -> 查询server_name失败,返回403

curl -i “127.0.0.1/“ -> 查询server_name失败,返回403