1. location中的相关修饰符!
修饰符 | 功能 |
---|---|
= | 精确匹配 |
- | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
1.1 查找顺序优先级为!!!
- 带有=的精确匹配优先
- 正则表达式按照他们在配置文件中定义的顺序
- 带有^~ 修饰符的,开头匹配
- 带有~ 或~*修饰符的,如果正则表达式与URI匹配
- 没有修饰符的精确匹
- ( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
配
2. 测试!!!
2.1 当没有修饰符的时候!!
[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
location /abc {
echo "abc";
}
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcd
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc\?p1\=1\&p2\=3
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc/
abc
2.2 精确匹配!!!
[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
location /abc {
echo "abc";
}
location = /abcd {
echo "abcd";
}
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcd
abcd
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdfg
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABC
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
2.3 区分大小写!!!
[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
location /abc {
echo "abc";
}
location = /abcd {
echo "abcd";
}
location ~ ^/abcdef$ {
echo "abcdef";
}
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcd
abcd
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdef
abcdef
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdeF
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/AbcdeF
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCdeF
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
2.4 不区分大小写!!
[root@localhost nginx-1.20.1]# vim /usr/local/nginx/conf/nginx.conf
location ~* ^/abcdefg${
echo "abcdefg";
}
[root@localhost nginx-1.20.1]# nginx -s reload
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdefg
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCDEFG
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCdefg
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABC
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
2.5 优先级!!!
location /abc {
echo "abc";
}
location = /abc {
echo "abcd";
}
location ~ ^/abcdef$ {
echo "abcdefg";
}
location ~* ^/abcdef$ {
echo "abcdef";
}
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abc
abcd
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdergh
abc
[root@localhost nginx-1.20.1]# curl 192.168.160.150/abcdef
abcdefg
[root@localhost nginx-1.20.1]# curl 192.168.160.150/ABCDEF
abcdef