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