nginx location优先级
网上查了下location的优先级规则,但是很多资料都说的模棱两可,自己动手实地配置了下,下面总结如下。
1. 配置语法
1> 精确匹配
location = /test {
...
}
2> 前缀匹配
- 普通前缀匹配
location /test {
...
}
- 优先前缀匹配
location ^~ /test {
...
}
3> 正则匹配
- 区分大小写
location ~ /test$ {
...
}
- 不区分大小写
location ~* /test$ {
...
}
2. 配置实例
1> 多个前缀匹配,访问/test/a,则先记住最长的前缀匹配,并继续匹配
location / {
root html;
index index.html index.htm;
}
location /test {
return 601;
}
location /test/a {
return 602;
}
命中的响应状态码
602
2> 前缀匹配和正则匹配,访问/test/a,则命中正则匹配
location / {
root html;
index index.html index.htm;
}
location /test/a {
return 601;
}
location ~* /test/a$ {
return 602;
}
命中的响应状态码
602
3> 优先前缀匹配和正则匹配,访问/test/a,则命中优先前缀匹配,终止匹配
location / {
root html;
index index.html index.htm;
}
location ^~ /test/a {
return 601;
}
location ~* /test/a$ {
return 602;
}
命中的响应状态码
601
4> 多个正则匹配命中,访问/test/a,则使用第一个命中的正则匹配,终止匹配
location / {
root html;
index index.html index.htm;
}
location ~* /a$ {
return 601;
}
location ~* /test/a$ {
return 602;
}
命中的响应状态码
601
5> 精确匹配和正则匹配,访问/test,精确匹配优先
location / {
root html;
index index.html index.htm;
}
location ~* /test {
return 602;
}
location = /test {
return 601;
}
命中的响应状态码
601
3. 总结:
- 搜索优先级:
精确匹配 > 字符串匹配( 长 > 短 [ 注: ^~ 匹配则停止匹配 ]) > 正则匹配( 上 > 下 )
- 使用优先级:
精确匹配 > (^~) > 正则匹配( 上 > 下 )>字符串(长 > 短)
解释:
- 先搜索有没有精确匹配,如果匹配就命中,终止匹配。
- 索前缀匹配,命中则先记住(可能命中多个),继续往下搜索,如果有优先前缀匹配符号“^~”,则命中优先前缀匹配,不再去检查正则表达式;反之则继续进行正则匹配,如果命中则使用正则表达式,如果没命中则使用最长的前缀匹配。
- 前缀匹配,长的优先;多个正则匹配,在上面的优先。
nginx location优先级的更多相关文章
- nginx——location 优先级
一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配 ...
- nginx location 优先级
location 顺序/优先级: location = > location 完整路径 > location ^~ 路径 > location ~,~* 正则顺序 > ...
- nginx location在配置中的优先级
location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配,不区分大小写^~ 表示普通字符匹配.使用前缀匹配.如果匹配成功,则不再匹配其他location.= 进行普通 ...
- NGINX location 在配置中的优先级
location表达式类型 ~ 表示执行一个正则匹配,区分大小写 ~* 表示执行一个正则匹配,不区分大小写 ^~ 表示普通字符匹配.使用前缀匹配.如果匹配成功,则不再匹配其他location. = 进 ...
- Nginx——location匹配与在配置中的优先级
1. location表达式类型 location ^~ /api/v7/ { proxy_next_upstream http_404 http_500 http_502 http_503 http ...
- (转)nginx location在配置中的优先级
原文:https://www.bo56.com/nginx-location%E5%9C%A8%E9%85%8D%E7%BD%AE%E4%B8%AD%E7%9A%84%E4%BC%98%E5%85%8 ...
- nginx location的优先级
原来一直以为location的优先级是先后顺序,结果有次项目中傻眼了,赶紧百度一下,下面的内容参考了这个链接 location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配 ...
- nginx location配置
nginx location配置 location在nginx中起着重要作用,对nginx接收到的请求字符串进行处理,如地址定向.数据缓存.应答控制.代理转发等location语法location ...
- nginx location匹配规则
谢谢作者的分享精神,原文地址:http://www.nginx.cn/115.html location匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹 ...
随机推荐
- (转)每天一个linux命令(1):ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...
- BZOJ4415:[SHOI2013]发牌(线段树)
Description 假设一开始,荷官拿出了一副新牌,这副牌有N张不同的牌,编号依次为1到N.由于是新牌,所以牌是按照顺序排好的,从牌库顶开始,依次为1, 2,……直到N,N号牌在牌库底.为了发完所 ...
- Codeforces Round #431 (Div. 2)
A. Odds and Ends Where do odds begin, and where do they end? Where does hope emerge, and will they e ...
- ubuntu安装完整版的vim
apt-get remove vim-commonapt-get install vim
- 【luogu P1801 黑匣子_NOI导刊2010提高(06)】 题解
题目链接:https://www.luogu.org/problemnew/show/P1801 替罪羊树吼啊! #include <cstdio> #include <cstrin ...
- 【luogu P1111 公路修建】 题解
题目链接:https://www.luogu.org/problemnew/show/P1111 考察并查集,运用kruskal的思想很好做.注意几个小问题即可. #include<iostre ...
- php如何实现登陆后返回原页面
访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面项目需求 访问网站页面时,有的页面需要授权才能访问,这时候就会要求 ...
- MVC5 模型 生成EF
在看本篇之前请先去了解一下EF以及如何利用模型生成数据库 https://i.cnblogs.com/posts?categoryid=1107227 看Code First就可以了. 等你了解了E ...
- JavaScript中的Map和Set
JavaScript的默认对象表示方法{}可以视为其他语言中的Map或者Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串,但实际上Numbe ...
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...