网上查了下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. HDU 6034 6038

    6034:给每个字母26进制的贪心.例如一个字母 c = 7*26^89 + 6*26^50.... 这个字符串有10^5长度.普通的大整数会超时,这里要稀疏这个大数一个pair<int,int ...

  2. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  3. 使用ParseExact方法将字符串转换为日期格式

    实现效果: 知识运用: DateTime结构的ParseExact方法 public static DateTime ParseExact(string s,string format,IFormat ...

  4. ThreadLocal 例子

    /** * 一个ThreadLocal代表一个变量,故其中里只能放一个数据,有两个变量都要线程内共享,则要定义两个ThreadLocal. */ public class ThreadLocalTes ...

  5. Javascript中==和===的区别

     一.JavaScript"=="的作用 1.当==两边的内容是字符串时,则比较字符串的内容是否相等. 2.当==两边的内容是数字时,则比较数字的大小是否相等. 3.当==两边的内 ...

  6. 【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)

    洛谷P1731:https://www.luogu.org/problemnew/show/P1731 思路 三重剪枝 当前表面积+下一层表面积如果超过最优值就退出 当前体积+下一层体积如果超过总体积 ...

  7. yarn下资源配置

    http://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.0.6.0/bk_installing_manually_book/content/rpm-ch ...

  8. 读取静态的json文件

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

  9. IPv4和IPv6的兼容问题

    一网络拓扑 Ipv6网络1 路由器A IPv4网络 路由器B IPv6网络2 二知识补充 [注]双协议栈主机(路由器A.B)通过域名解析器区分传过来的是IPv4还是IPv6 三处理技术 双协议栈 Ip ...

  10. Linux运维工作中需要掌握的知识

    说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了.linux系统如果是学习可以选用redhat或centos,特别是cent ...