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>

~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式

查找顺序和优先级:由高到底依次为

  1. 带有=的精确匹配优先
  2. 正则表达式按照他们在配置文件中定义的顺序
  3. 带有^~修饰符的,开头匹配
  4. 带有~~*修饰符的,如果正则表达式与URI匹配
  5. 没有修饰符的精确匹配

优先级次序如下:

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值