详细解析 nginx uri 如何匹配 location 规则
location 是 nginx 配置中出现最频繁的配置项,一个 uri 是如何与多个 location 进行匹配的?
在有多个 location 都匹配的情况下,如何决定使用哪一个 location 作为匹配结果?
清晰内部机制之后,疑问自然迎刃而解。
location 规则类型
根据官方文档,location 配置语法如下
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
其语法对应了 location 规则的 5 种类型:
prefix
前缀匹配规则,如果 uri 包含相应的前缀,则匹配成功。
语法上, location 之后单纯跟上 uri,没有任何操作符。
location /api/v1 {
....
}
exact uri
完全匹配规则,如果 uri 和规则完全相同,则匹配成功。
语法上,使用 = 操作符。
location = /api/v1 {
....
}
case-sensitive regex
区分大小写的正则表达式匹配规则,如果 uri 与正则表达式相匹配,则匹配成功。
正则表达式使用 pcre library,语法和 perl 兼容。
语法上,使用 ~ 操作符。
这里有一个细节,uri 作为一个 string,只要 regex 匹配其中的一部分,即可算作匹配。
比如有如下的规则,
location ~ /api/v1 {
....
}
可以匹配 uri
- /api/v1/login
- /pod/api/v1
如果想要匹配整个 string,则要使用 ^ $。
比如,
location ~ ^/api/v1.*$ {
....
}
可以匹配 uri
- /api/v1/login
不可以匹配
- /pod/api/v1
case-insensitive regex
规则同上,区别只在于,正则表达式不区分大小写。
不区分大小写的正则表达式匹配规则,如果 uri 与正则表达式相匹配,则匹配成功。
语法上,使用 ~* 操作符。
location ~* /api/v1 {
....
}
disable regex prefix
匹配流程与 prefix 规则相同,有一点区别在于,如果最长匹配是当前规则,则之后不进行 正则表达式 规则的搜索。
这一点可能有些难以理解,后面会详细讲解。
语法上,使用 ^~ 操作符。
location ^~ /api/v1 {
....
}
uri 如何选择 location
前面讲到,location 规则有 5 种类型,那么 uri 如何在多种不同类型的 location 规则之间,
最终选择到唯一的 location 规则呢?
根据官方文档的描述,详细规则如下:
- 如果存在 exact uri 规则与 uri 匹配,至步骤 6
- 在所有 prefix 规则和 disable regex prefix 规则中进行匹配(与这些规则定义的顺序无关),
如果没有匹配到规则,至步骤 3;如果存在匹配的规则,选择出最长匹配 uri 的规则:- 如果规则是 disable regex prefix 类型,至步骤 6
- 如果规则是 prefix 类型,记住当前匹配的 prefix 规则,选为待定,至步骤 3
- 逐个遍历 case-sensitive regex 规则和 case-insensitive regex 规则(按照这些规则定义的前后顺序):
- 如果规则匹配,则遍历终止,至步骤 6
- 如果规则没有匹配,则继续
- 如果之前有 prefix 规则条目被选择为待定,至步骤 6
- 匹配失败,返回404,结束
- 选择当前规则,使用其配置,结束
图解

draw by draw.io, 源文件
FAQ
有人可能会困惑,如果 prefix 与 disable regex prefix 规则相同,比如,
location /static/ {
....
}
location ^~ /static/ {
....
}
最终会匹配哪一条规则?是否应该继续查找 regex 规则?
实际上,这种情况 nginx 会报错,nginx: [emerg] duplicate location "/static/" in /etc/nginx/conf.d/location.conf,
所以这种情况是不用考虑的。
示例
示例项目在 nginx example,
参考 readme 文件,在本地启动 nginx server。
其中对 nginx 的配置如下,使用 curl 进行检测。
exact uri 规则先于所有规则(与规则定义的顺序无关),所以这里匹配了 = / exact uri 规则而不是 / prefix 规则。
$ curl http://localhost:8080/
uri "/": exact match "location = /"
exact uri 规则先于所有规则(与规则定义的顺序无关),所以这里没有匹配 ^~ /static/ disable regex prefix 和 ~* \.PNG$,~ \.png$ regex 规则。
正是因为 exact uri 规则的这种属性,如果有高频率使用的 uri,建议使用 exact uri 匹配,加快匹配速度。
$ curl http://localhost:8080/static/logo.png
uri "/static/logo.png": exact uri match "location = /static/logo.png"
prefix 规则中,匹配 uri 长度最长的规则会选中,所以没有选中 / prefix 规则。同样的,从这一点来看,/api 和 /api/ 是不一样的。
$ curl http://localhost:8080/api
uri "/api": prefix match "location /api"
$ curl http://localhost:8080/api/
uri "/api/": prefix match "location /api/"
$ curl http://localhost:8080/api/v1
uri "/api/v1": prefix match "location /api/"
如果匹配了 ^~ /static/ disable regex prefix 规则,则不再去搜索之后的 regex 规则。
$ curl http://localhost:8080/static/thinkpad.png
uri "/static/thinkpad.png": disable regex prefix match "location ^~ /static/"
在 regex 规则的匹配中,定义的先后顺序是重要的,所以 ~ \.png$ case-sensitive 规则永远不会被匹配。
$ curl http://localhost:8080/files/large.png
uri "/files/large.png": case-insesitive regex match "location ~* \.PNG(dollar)"
$ curl http://localhost:8080/files/large.PNG
uri "/files/large.PNG": case-insesitive regex match "location ~* \.PNG(dollar)"
即使先匹配了 /api/ prefix 规则作为待定,但是也匹配了 regex 规则,优先使用 regex 规则。
$ curl http://localhost:8080/api/v1/file/logo.png
uri "/api/v1/file/logo.png": case-insesitive regex match "location ~* \.PNG(dollar)"
存在 / prefix 规则是有益的,收容所有未明确定位的 uri,再做错误处理。
$ curl http://localhost:8080/no-where
uri "/no-where": prefix match "location /"
写在最后
详细解析 nginx uri 如何匹配 location 规则的更多相关文章
- Nginx日志参数、location匹配规则、设置密码
1.三个参数 a)$http_referer:记录此次请求是从哪个链接访问过来的: 是直接访问,还是从其他网站跳转过来的. 例如:访问:http://www.etiantian.com/,其页面首页是 ...
- Nginx location指令匹配顺序规则
location匹配命令 1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何 ...
- nginx教程1:location 匹配规则
worker_process # 表示工作进程的数量,一般设置为cpu的核数 worker_connections # 表示每个工作进程的最大连接数 server{} # 块定义了虚拟主机 liste ...
- nginx里面的location 规则匹配
nginx location语法 ~ # 区分大小写的正则匹配 location ~ \.(gif|jpg|png|js|css)$ { #规则D } ~* # 不区分大小写的正则匹配(和~的功能相同 ...
- Nginx配置请求转发location及rewrite规则
一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这 ...
- nginx配置文件结构及location块语法规则
一. nginx配置文件结构介绍 二. location语法规则: 用法示例: location [=|~|~*|^~] /uri/ { … } # 讲解如下: 1. = 开头表示精确匹配 2. ...
- Nginx rewrite跳转 location匹配
目录: 一.常用的Nginx 正则表达式 二.location 三.rewrite 一.常用的Nginx 正则表达式 1 ^ :匹配输入字符串的起始位置 2 $ :匹配输入字符串的结束位置 3 * : ...
- [日常] nginx与location规则
=========================================================================2018年3月28日 记录: location = / ...
- Nginx Location规则
Nginx由内核和模块组成,其中内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端的请求映射到一个location block,而location是Nginx配置中的一个指令 ...
随机推荐
- python基础学习16----模块
模块(Module)的引入 import 模块名 调用模块中的函数的方式为 模块名.函数名 这种方式引入会相当于将模块中代码执行一遍,所以如果引入的模块中有输出语句,那么只写import 模块名,运行 ...
- Android之间传递数据包
在Android中 ,我们知道,两个activity之间通讯要用到Intent类,传递简单数据的方式我们也已经知道了.那么,如何在两个activity之间传递数据包呢,这就要用到我们的Bundle类了 ...
- ansible 碎记录
https://www.zhukun.net/archives/8167 ansible -i new/hosts new -m authorized_key -a "user=root k ...
- centos中,tomcat项目创建文件的权限
参考文章:https://cloud.tencent.com/info/5f02caa932fd6dbfc46a3bb01af135e0.html 我们在centos中输入umask,会看到输出002 ...
- centos httpd开启https服务并申请免费https
安装httpd yum -y install httpd httpd配置文件路径 /etc/httpd/conf/httpd.conf 安装OpenSSL yum install mod_ssl o ...
- UltraISO制作使用(服务器装机u盘制作)
1.准备工作: 1)U盘一个,需要格式化(大于4G,毕竟ISO文件就已经大于4G了) 2)CentOS7.1 iso文件一个(去这里下载:http://www.centoscn.com/) 3)Ult ...
- python第四十四课——继承性之单继承
2.继承性 继承: 使用场景: 1).生活层面:... 2).计算机层面: 两部分组成,一部分我们称为父类(基类.超类.superclass),另一部分我们称为子类(派生类.subclass), 子类 ...
- 面向对象的JavaScript --- 动态类型语言
面向对象的JavaScript --- 动态类型语言 动态类型语言与面向接口编程 JavaScript 没有提供传统面向对象语言中的类式继承,而是通过原型委托的方式来实现对象与对象之间的继承. Jav ...
- BZOJ3236:[AHOI2013]作业(莫队,分块)
Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...
- Kubernetes1.91(K8s)安装部署过程(五)--安装flannel网络插件
node节点需要安装flannel网络插件才能保证所有的pod在一个局域网内通信,直接使用yum安装即可,版本是0.7.1. 1.安装flannel插件: 注意是2个node节点都需要安装,都需要修改 ...