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匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹 ...
随机推荐
- kill 使用当前数据库的所有session
--在维护中经常需要将某一数据库所有进程都杀掉,手工杀有点太费事.写了一个存储过程 --usage:proc_kill 'PSADBA' create proc proc_kill(@db varch ...
- luogu P1121 环状最大两段子段和
嘟嘟嘟 一道说难也难说简单也简单的dp题. 我觉得我的(有篇题解)做法就属于特别简单的. 平时遇到环的问题都是断环为链,但这道题给了一种新的思路. 观察一下,最后的答案无非就这两种:xxx--xx-- ...
- POJ 2365【YY水题】
题目链接:POJ 2365 Rope Rope Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7488 Accepted ...
- [转]Qt中ui文件的使用
用designer设计的*.ui文件可以通过uic工具转换为*.h文件(在编译时也会自动生成这样一个ui_*.h文件),有了这个.h文件就可以直接按照纯C++的方式对其中的类进行调用.ui文件的使用就 ...
- Hibernate的优缺点:
1.Hibernate的优缺点:优点:1.程序更加面向对象:2.提高了生产率:3.方便移植(修改配置文件):4.无侵入性.缺点:1.效率比JDBC略差:2.不适合批量操作. 总的来说,hibernat ...
- 【洛谷P1774】最接近神的人
最接近神的人_NOI导刊2010提高(02) 用类似于桶的方法,树状数组记录原序列的某位置之前已经插入了多少个数, 插入时树状数组单点加1即可 先排一遍序,从大到小插入所有数在原序列的位置, 统计每次 ...
- Java数据结构——二叉树 增加、删除、查询
//二叉树系统 public class BinarySystem { public static void main(String[] args) { BinaryDomain root = nul ...
- 优雅的QSignleton (三) 通过属性器实现Singleton
接下来介绍,不通过继承的方式实现单例模式.大家都出去嗨了,而我却在家码代码... 代码如下: MonoSingletonProperty.cs namespace QFramework.Example ...
- 数据恢复顾问(DRA)
(1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文件和(某些环境下)控制文件的损坏,它不提供spfile和logfile的修复 ...
- httpd的prefork、worker、event
Apache(httpd) 有3种核心MPM(Multi-Processing Module,多进程处理模块)工作模式,分别是prefork,worker和event,其中httpd-2.2的even ...