Nginx 修饰符 Location 详解

概述#

location 指令可以用在虚拟服务器 server 部分,并且意味着提供来自客户端的 URI 或者内部重定向访问。

location 的定义如下:

Copy
location [modifier] uri {...} 或者是命名 location location @name {...}

命名 location 仅对内部访问重定向,在进入一个 location 之前,它会保留请求的 URI 部分。命名 location 只能够在 server 级别定义。

location 修饰符类型#

= 修饰符#

该修饰符使用精确匹配并且终止搜索。

精确匹配 /test 路径。

Copy
location = /test { echo "matching"; }

注:这里的 echo 需要安装 echo-nginx-module 模块。可以参考文章: https://www.cnblogs.com/yxhblogs/p/12901575.html

测试一:完全匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test matching

测试二:路径大写不匹配(如果操作的文件系统大小写不敏感,也会匹配)。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

测试三:忽略路径上的参数,会匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea matching

测试四:带 / 结尾的,不会匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

测试五:路径不一致,不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1 <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

~ 修饰符#

该修饰符使用区分大小写的正则表达式匹配

表示必须以 / 开始,以 $ 结束,中间是 test。

Copy
location ~ ^/test$ { echo "matching"; }

测试一:匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test matching

测试二:不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

测试三:忽略路径上的参数,会匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea matching

测试四:不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

测试五:不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1 <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

~* 修饰符#

该修饰符使用不区分大小写的正则表达式匹配

不区分大小写 test 路径

Copy
location ~* ^/test$ { echo "matching"; }

测试一:匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test matching

测试二:匹配,大小写不敏感。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST matching

测试三:忽略路径上的参数,会匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea matching

测试四:不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

测试五:不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1 <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

^~ 修饰符#

如果该 location 是最佳的匹配,那么对于匹配这个 location 的字符串,该修饰符不再进行正则表达式检测。注意:这不是一个正则表达式匹配,它的目的是优先于正则表达式的匹配。

Copy
location ^~ /test { echo "matching"; }

测试一:匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test matching

测试二:不匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.9.9</center> </body> </html>

测试三:匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea matching

测试四:匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/ matching

测试五:匹配。

Copy
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1 matching
posted @   Yxh_blogs  阅读(841)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示
CONTENTS