Nginx 修饰符 Location 详解
概述
location 指令可以用在虚拟服务器 server 部分,并且意味着提供来自客户端的 URI 或者内部重定向访问。
location 的定义如下:
location [modifier] uri {...}
或者是命名 location
location @name {...}
命名 location 仅对内部访问重定向,在进入一个 location 之前,它会保留请求的 URI 部分。命名 location 只能够在 server 级别定义。
location 修饰符类型
= 修饰符
该修饰符使用精确匹配并且终止搜索。
精确匹配 /test 路径。
location = /test {
echo "matching";
}
注:这里的 echo 需要安装 echo-nginx-module 模块。可以参考文章: https://www.cnblogs.com/yxhblogs/p/12901575.html
测试一:完全匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching
测试二:路径大写不匹配(如果操作的文件系统大小写不敏感,也会匹配)。
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>
测试三:忽略路径上的参数,会匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching
测试四:带 / 结尾的,不会匹配。
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>
测试五:路径不一致,不匹配。
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。
location ~ ^/test$ {
echo "matching";
}
测试一:匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching
测试二:不匹配。
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>
测试三:忽略路径上的参数,会匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching
测试四:不匹配。
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>
测试五:不匹配。
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 路径
location ~* ^/test$ {
echo "matching";
}
测试一:匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching
测试二:匹配,大小写不敏感。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/TEST
matching
测试三:忽略路径上的参数,会匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching
测试四:不匹配。
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>
测试五:不匹配。
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 的字符串,该修饰符不再进行正则表达式检测。注意:这不是一个正则表达式匹配,它的目的是优先于正则表达式的匹配。
location ^~ /test {
echo "matching";
}
测试一:匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test
matching
测试二:不匹配。
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>
测试三:匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test?name=yxhsea
matching
测试四:匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test/
matching
测试五:匹配。
ubuntu@VM-0-10-ubuntu:~/nginx$ curl http://localhost/test1
matching

浙公网安备 33010602011771号