网上查了下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. 设计模式——策略模式(Strategy Pattern)

    写在前面: 直接将书中的例子用来作为记录自己学习的成果,不知道这样好不好,如果给原作者带来什么不利的影响不妨告知一声,我及时删掉. UML图: 抽象策略:Strategy package com.cn ...

  2. POJ 3067 Japan 【树状数组经典】

    题目链接:POJ 3067 Japan Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32076   Accep ...

  3. servlet三种方式实现servlet接口

    简单介绍 Servlet接口实现类 1.Servlet接口SUN公司定义了两个默认实现类,分别为:GenericServlet.HttpServlet. 2.HttpServlet指能够处理HTTP请 ...

  4. 简单的使用git克隆上传创建下载删除

    首先我们下载个git 官网:https://git-scm.com/ 我这里下的是64-bit windows setup 下载完成后安装: 一直下一步就好 安装完成后进行打开! 现在我们需要去官网注 ...

  5. Python 学习笔记(七)Python字符串(三)

    常用字符串方法 split()  分割字符串,指定分隔符对字符串进行分割 join()   将序列中的元素以指定的字符连接生成一个新的字符串 str.strip() 用于移除字符串头尾指定的字符(默认 ...

  6. PL/SQL 数组的使用

    一.固定数组 1.在模式(schema)级创建VARRAY类型 语法: CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) OF <elem ...

  7. ABAP术语-Business Object Builder

    Business Object Builder 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/09/1031357.html Tool fo ...

  8. 【解决】MongoDB 线上业务处理,数据去重脚本实现

    mongo客户端工具下载  https://robomongo.org/download   线上业务,k线 展示出现问题,相同时间戳的数据多次插入导致数据不真实,后经排查发现是每次都是写的四条数据, ...

  9. NC使用技巧

    1. NC 1.1概述 1.1.1 优点: 1)网络工具中的瑞士军刀 2)侦听模式/传输模式. 3)可代替telnet获取banner信息. 4)传输文件/目录. 5)传输文本信息. 6)加密传输文件 ...

  10. Ubuntu16.04采用FastCGI方式部署Flask web框架

    1    部署nginx 1.1    安装nginx服务 root@desktop:~# apt-get install nginx -y 1.2    验证nginx服务是否启动 root@des ...