详细解析 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配置中的一个指令 ...
随机推荐
- abp框架里使用Redis
首先引用 nuget Abp.RedisCache 在 appsettings.json加上Redis服务器配置 "RedisCache": { "ConnectionS ...
- PyCharm导入模块报No model named
PyCharm导入模块报No model named 引言 在PyCharm中同目录下import其他模块,出现No model named ...的报错,但实际可以运行的情况. 这很可能是因为PyC ...
- 使用Swoole 构建API接口服务
网上类似的文章已经很多了,我也是刚入门.从头开始学习.所以如果重复写文章阐释,反而会浪费时间,于是就自己动手构建了一个demo,使用swoole 的TCP 服务器接受TCP客户端的发来的http请求, ...
- C#中单问号(?)和双问号(??)的用法整理
1.单问号(?) 1.1 表示Nullable类型 C#2.0里面实现了Nullable数据类型 //A.比如下面一句,直接定义int为null是错误的,错误提示为无法将null转化成int,因为后者 ...
- 如何安装Firebug
Firebug是firefox下的一个插件,能够调试所有网站语言,如Html,Css等,但FireBug最吸引我的就是javascript调试功能,使用起来非常方便,而且在各种浏览器下都能使用(IE, ...
- jumperserver docker部署
最近在考虑 系统安全问题,jumperserver 用docker 部署比较简单1. 安装redis2.安装mariadb create database jumpserver charset='ut ...
- 网站横幅切换jquery 插件
最近做一个web项目,站点首页需要像 百度统计 页面类似的切换横幅,有兴趣的可以先看看它的效果,这样就比较容易理解为什么我单独做个插件.其实类似的 jquery 插件网上类似的多的去了,随便网上下了两 ...
- tusen 刷题
//1.single number和变体 //2.lru lfu 3.给一个正整数集合,求一个和最大且能被3整除的子集.Follow up: 如果集合里有正有负 4.leetcode200-numbe ...
- 20165302Exp0 Kali安装 Week1
一,下载 下载网址https://www.kali.org/downloads/ 二,安装(安装过程中有一部分没有截图,因此没有贴上) 创建虚拟机 选择Linux,版本Ubuntu 一直下一步,最后点 ...
- leetcode18—4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...