网上查了下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优先级的更多相关文章

  1. nginx——location 优先级

    一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配 ...

  2. nginx location 优先级

    location 顺序/优先级:     location = > location 完整路径 > location ^~ 路径 > location ~,~* 正则顺序 > ...

  3. nginx location在配置中的优先级

    location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配,不区分大小写^~ 表示普通字符匹配.使用前缀匹配.如果匹配成功,则不再匹配其他location.= 进行普通 ...

  4. NGINX location 在配置中的优先级

    location表达式类型 ~ 表示执行一个正则匹配,区分大小写 ~* 表示执行一个正则匹配,不区分大小写 ^~ 表示普通字符匹配.使用前缀匹配.如果匹配成功,则不再匹配其他location. = 进 ...

  5. Nginx——location匹配与在配置中的优先级

    1. location表达式类型 location ^~ /api/v7/ { proxy_next_upstream http_404 http_500 http_502 http_503 http ...

  6. (转)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 ...

  7. nginx location的优先级

    原来一直以为location的优先级是先后顺序,结果有次项目中傻眼了,赶紧百度一下,下面的内容参考了这个链接 location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配 ...

  8. nginx location配置

    nginx location配置   location在nginx中起着重要作用,对nginx接收到的请求字符串进行处理,如地址定向.数据缓存.应答控制.代理转发等location语法location ...

  9. nginx location匹配规则

    谢谢作者的分享精神,原文地址:http://www.nginx.cn/115.html location匹配命令 ~      #波浪线表示执行一个正则匹配,区分大小写~*    #表示执行一个正则匹 ...

随机推荐

  1. JVM原理:4 运行期优化

    JVM运行期优化 Java程序在运行的期间,可能会有某个方法或者代码块的运行特别频繁时,就会把这些代码认定为“热点代码”.为了提高热点代码的执行效率,在运行时JVM会将这些代码编译成与本地平台相关的机 ...

  2. 「bzoj3956: Count」

    题目 刚开始并没有看懂题意于是痛苦的挣扎了好久 题意是这样的 问\([l,r]\)有多少对\((i,j)\)满足\(a_i\)和\(a_j\)恰好是\(a_i...a_j\)中严格最大的两个数 强制在 ...

  3. 2018.11.24 struts2中的OGNL表达式及两者的结合

    OGNL表达式 OGNL:对象视图导航语言. ${user.addr.name} 这种写法就叫对象视图导航. OGNL不仅仅可以视图导航.支持比EL表达式更加丰富的功能. 理解图示 使用OGNL准备工 ...

  4. 2018.11.10 Mac设置Eclipse的 .m2文件夹是否可见操作&&Mac系统显示当前文件夹的路径设置

    第一行就是设置为可见的记得要重启Finder不然是没有效果的 第二行就是设置为不可见的 打开"终端"(应用程序->实用工具),输入以下两条命令: defaults write ...

  5. fastJson简单实用

    public class FastJsonText { @Test public void text(){ User user1 = new User(); user1.setName("健 ...

  6. Softmax vs. Softmax-Loss: Numerical Stability

    http://freemind.pluskid.org/machine-learning/softmax-vs-softmax-loss-numerical-stability/ softmax 在 ...

  7. css块元素的 display 属性 inline-block 的应用

    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < ...

  8. AngularJS web应用程序

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. JS JavaScript深拷贝、浅拷贝

    浅拷贝:浅拷贝只是复制了内存地址,如果原地址中的对象改变了,浅拷贝出来的对象也会相应改变. 深拷贝:开辟了一块新的内存存放地址和地址指向的对象,原地址的任何对象改变了,深拷贝出来的对象不变. 浅拷贝数 ...

  10. element组件dialog关闭时Message消息提示抖动问题

    在页面内容较多,出现滚动条时使用element组件里的dialog组件,当关闭dialog组件的同时弹出Message消息提示时,Message会抖动一下. 在页面有滚动条的情况先打开dialog时, ...