location应用
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {......}
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符
[root@localhost conf]# vim nginx.conf
location /zdj {
echo "zdj";
}
[root@localhost conf]# nginx -s reload
[root@localhost ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/zdj\?p1\=11\&p2\=22
zdj
=:表示必须与指定的模式精确匹配
[root@localhost conf]# vim nginx.conf
location /zdj {
echo "zdj";
}
location /zdjd {
echo "zdjd";
}
[root@localhost conf]# nginx -s reload
[root@localhost ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/zdjd
zdjd
[root@localhost ~]# curl 192.168.100.123/zdwads
<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 ~]# curl 192.168.100.123/zdjijwdiawd
zdj
[root@localhost ~]# curl 192.168.100.123/zdj\?\p1\=11\&p2\=22
zdj
~:表示指定的正则表达式要区分大小写
[root@localhost conf]# vim nginx.conf
location ~ ^/zdj$ {
echo "zdj";
}
[root@localhost conf]# nginx -s reload
[root@localhost ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/zdjR
<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 ~]# curl 192.168.100.123/zdJ
<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 ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/zdj\?p1\=11\&p2\=22
zdj
~*:表示指定的正则表达式不区分大小写
[root@localhost conf]# vim nginx.conf
location ~* ^/zdj$ {
echo "zdj";
}
[root@localhost conf]# nginx -s reload
[root@localhost ~]# curl 192.168.100.123/zdJ
zdj
[root@localhost ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/ZDJ\?p1\=11\&p2\=22
zdj
查找顺序和优先级:由高到底依次为
1.带有=的精确匹配优先
2.正则表达式按照他们在配置文件中定义的顺序
3.带有^~修饰符的,开头匹配
4.带有或*修饰符的,如果正则表达式与URI匹配
5.没有修饰符的精确匹配
[root@localhost conf]# vim nginx.conf
location = /zdj {
echo "zdj";
}
location ~ /zdj {
echo "zdj";
}
location ~* ^/zdj$ {
echo "zdj";
}
[root@localhost conf]# nginx -s reload
[root@localhost ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/zdjsdaa
zdj
[root@localhost ~]# curl 192.168.100.123/zd
<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 conf]# vim nginx.conf
location = /zdj {
echo "zdj";
}
location ~ ^/zdj {
echo "zdj";
}
location ~* ^/zdj$ {
echo "zdj";
}
[root@localhost conf]# nginx -s reload
[root@localhost ~]# curl 192.168.100.123/zdj
zdj
[root@localhost ~]# curl 192.168.100.123/zd
<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 = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )