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匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹 ...
随机推荐
- win8+VS2012搭建OpenGL超级宝典的环境
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/booirror/article/details/36957799 自从公司搬到腾讯附近,每天上班都迟 ...
- ZJOI2019Day2余姚中学游记(4.23~4.26)
前言 \(Day2\),又是一场噩梦. 前段时间去做了挺多十二省联考和\(HNOI2019\)的题目,还订正掉了\(Day1\)的\(T1\)和\(T2\)(\(T3\)动态\(DP\)完全不想订正啊 ...
- 【转】 ios的手势操作之UIGestureRecognizer浅析
一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withE ...
- 昨天刚看了Handler和HandlerThread这个东西,不明白为什么要用这么复杂的东西,而且Handler直接post的话好像还不是子线程运行。那我再开发的时候直接用Thread行不行?两个有什么区别?
Handler就是android中一个机制,主要是考虑到线程安全的! Handler是可以实现线程间通信的,LZ知道Android的UI线程不安全的吧,也就是说不可以在UI线程以外的其他线程对UI进行 ...
- 2018.12.30 Intellij IDEA设置main方法自动补全
Eclipse与 Intellij IDEA设置方法自动补全 1.首先,点击File-->Settings-->Editor-->Live Templates 设置你想输出的模板 右 ...
- fastJson简单实用
public class FastJsonText { @Test public void text(){ User user1 = new User(); user1.setName("健 ...
- cblas_sgemm cblas.h
BLAS(Basic Linear Algebra Subprograms)库,是用Fortran语言实现的向量和矩阵运算库,是许多数值计算软件库的核心, 但也有一些其它的包装, 如cblas是C语言 ...
- 关于eclipse中引入项目报错或者没有JRE System Library问题(jre报错)或者jre1.7(8)改为jre1.8(7)等问题
解决方法: 右键项目工程-->>properties->>java bulid path -->>>libraries -->>add libra ...
- com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭
com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭 解决方案: DBUtil公共方法如下: package ...
- tomcat的下载和启动
1.下载和解压 把下载好的压缩包解压,放到想刚的目录里 看一下目录: 目录介绍: 2. 启动和访问 启动步骤: 如果startup.bat 双击执行脚本一闪而过,解决方法: 第一可能是:没有配置JAV ...