location区段
Posted 卑微小胡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了location区段相关的知识,希望对你有一定的参考价值。
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {......}
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符表示必须以指定模式开始,如:
location /abc {
echo "test";
}
那么如下内容就可正确匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc
test
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc\\?p1\\=11\\&p2\\=22
test
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc/
test
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abcsdsd
test
=:表示必须与指定的模式精确匹配,如:
location = /abc {
echo "test";
}
那么如下内容就可正确匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc
test
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc\\?p1\\=11\\&p2\\=22
test
如下内容则无法匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc/ac
<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.96.133/abcss
<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>
~:表示指定的正则表达式要区分大小写,如:
location ~ ^/abc$ {
echo "test";
}
那么如下内容就可正确匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc
test
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc\\?p1\\=11\\&p2\\=22
test
如下内容则无法匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abcdsd
<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.96.133/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>
~*:表示指定的正则表达式不区分大小写,如:
location ~* ^/abc$ {
echo "test";
}
那么如下内容就可正确匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc
test
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc\\?p1\\=11\\&p2\\=22
test
如下内容则无法匹配:
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abc/sds
<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.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/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>
[root@localhost nginx-1.20.1]# curl 192.168.96.133/abcdsd
<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>
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
查找顺序和优先级:由高到底依次为
- 带有
=
的精确匹配优先 - 正则表达式按照他们在配置文件中定义的顺序
- 带有
^~
修饰符的,开头匹配 - 带有
~
或~*
修饰符的,如果正则表达式与URI匹配 - 没有修饰符的精确匹配
优先级次序如下:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
以上是关于location区段的主要内容,如果未能解决你的问题,请参考以下文章