开篇
这篇文章主要是和Nginx之location 匹配规则详解配套的练习的例子,可以在自己的电脑上测试验证加深Nginx location匹配规则理解,对于例子中不懂的地方可以看看上一篇文章。
Nginx的匹配规则顺序是
- 1、location 的匹配顺序是“先匹配正则,再匹配普通”。
矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”。我这么说,大家一定会反驳我,因为按“先匹配普通,再匹配正则”解释不了大家平时习惯的按“先匹配正则,再匹配普通”的实践经验。这里我只能暂时解释下,造成这种误解的原因是:正则匹配会覆盖普通匹配(实际的规则,比这复杂,后面会详细解释)。
- 2、location 的执行逻辑跟 location 的编辑顺序无关。
矫正:这句话不全对,“普通 location ”的匹配规则是“最大前缀”,因此“普通 location ”的确与 location 编辑顺序无关;但是“正则 location ”的匹配规则是“顺序匹配,且只要匹配到第一个就停止后面的匹配”;“普通location ”与“正则 location ”之间的匹配顺序是?先匹配普通 location ,再“考虑”匹配正则 location 。注意这里的“考虑”是“可能”的意思,也就是说匹配完“普通 location ”后,有的时候需要继续匹配“正则 location ”,有的时候则不需要继续匹配“正则 location ”。两种情况下,不需要继续匹配正则 location :( 1 )当普通 location 前面指定了“ ^~ ”,特别告诉 Nginx 本条普通 location 一旦匹配上,则不需要继续正则匹配;( 2 )当普通location 恰好严格匹配上,不是最大前缀匹配,则不再继续匹配正则。
- 3、总结一句话: “正则 location 匹配让步普通 location 的严格精确匹配结果;但覆盖普通 location 的最大前缀匹配结果”
练习一、先普通 location ,再正则 location
- 1、nginx配置
server {
listen 8000;
server_name localhost;
location / {
root html;
index index.html index.htm;
deny all;
}
location ~ \.html$ {
root html;
allow all;
}
}
- 2、测试结果
URI 请求 | HTTP 响应 |
---|---|
curl http://localhost:8000/ | 403 Forbidden |
curl http://localhost:8000/index.html | Welcome to nginx! |
curl http://localhost:8000/index_notfound.html | 404 Not Found |
3、说明
curl http://localhost:8000/ 的结果是“ 403 Forbidden ”,说明被匹配到“ location / {..deny all;} ”了,原因很简单HTTP 请求 GET / 被“严格精确”匹配到了普通 location / {} ,则会停止搜索正则 location。
curl http://localhost:8000/index.html 结果是“ Welcome to nginx! ”,说明没有被“ location / {…deny all;} ”匹配,否则会 403 Forbidden ,但 /index.html 的确也是以“ / ”开头的,只不过此时的普通 location / 的匹配结果是“最大前缀”匹配,所以 Nginx 会继续搜索正则 location , location ~ .html$ 表达了以 .html 结尾的都 allow all; 于是接着就访问到了实际存在的 index.html 页面。
curl http://localhost:8000/index_notfound.html 同样的道理先匹配 location / {} ,但属于“普通 location 的最大前缀匹配”,于是后面被“正则 location ” location ~ .html$ {} 覆盖了,最终 allow all ; 但的确目录下不存在index_notfound.html 页面,于是 404 Not Found。
练习二、普通 location 的“隐式”严格匹配
- 1、Nginx配置
server {
listen 8000;
server_name localhost;
location / {
root html;
index index.html index.htm;
deny all;
}
location /exact/match.html {
root html;
allow all;
}
location ~ \.html$ {
root html;
allow all;
}
}
- 2、测试结果
URI 请求 | HTTP 响应 |
---|---|
curl http://localhost:8000/exact/match.html/ | 404 Not Found |
3、说明
“普通 location ”的“严格精确”匹配会终止对正则 location 的搜索。
“普通location 与 正则location的匹配规则:先匹配普通location ,再匹配正则location,但是如果普通 location的匹配结果恰好是“严格精确( exact match )”的,则Nginx不再尝试后面的正则location ;如果普通 location的匹配结果是“最大前缀”,则正则location的匹配覆盖普通location的匹配。也就是前面说的“正则 location让步普通location 的严格精确匹配结果,但覆盖普通location的最大前缀匹配结果”。
练习三、普通 location 的“显式”严格匹配和“ ^~ ” 前缀
- 1、Nginx配置
server {
listen 8000;
server_name localhost;
location ^~ / {
root html;
index index.html index.htm;
deny all;
}
location /exact/match.html {
root html;
allow all;
}
location /exact {
root html;
allow all;
}
location ~ \.html$ {
root html;
allow all;
}
}
- 2、测试结果
URI 请求 | HTTP 响应 |
---|---|
curl http://localhost:8000/ | 403 Forbidden |
curl http://localhost:8000/index.html | 403 Forbidden |
curl http://localhost:8000/index_notfound.html | 403 Forbidden |
curl http://localhost:8000/exact/match.html | 404 Not Found |
curl http://localhost:8000/exact/test.html | 404 Not Found |
3、说明
前三个http请求都是 403 Forbidden ,原因很简单所有请求都是以“ / ”开头,所以所有请求都能匹配上“ / ”普通location, location ^~ / {deny all;} 并终止正则搜索。
/exact/match.html的请求返回404 Not Found,是因为在普通location匹配规则当中,/exact/match.html 匹配到 location /exact/match.html {allow all;} 。
/exact/test.html的请求返回404 Not Found,是因为普通 location 的匹配原则是“最大前缀”,匹配上普通location的规则/exact {allow all;}
可以得出结论在匹配普通location和正则location的时候,如果普通location匹配的结果是不继续匹配正则location,那么匹配就到此结束了。
练习四、“ = ”前缀的使用
- 1、Nginx配置
server {
listen 8000;
server_name localhost;
location = / {
root html;
index index.html index.htm;
deny all;
}
location /exact/match.html {
root html;
allow all;
}
location ~ \.html$ {
root html;
allow all;
}
}
- 2、测试结果
URI 请求 | HTTP 响应 |
---|---|
curl http://localhost:8000/ | 403 Forbidden |
curl http://localhost:8000/index.html | Welcome to nginx! |
curl http://localhost:8000/index_notfound.html | 404 Not Found |
curl http://localhost:8000/exact/match.html | 404 Not Found |
3、说明
/ 返回403 Forbidden是因为匹配了普通location规则location = / {deny all;}
/index.html 和 index_notfound.html 匹配了正则location规则 ~ .html$ {allow all;},前者有对应的html文件返回Welcome to nginx!,后者没找到返回404 Not Found。
/exact/match.html匹配普通location的/exact/match.html,因为没有对应的html文件所以返回404 Not Found。
匹配原则仍然是先普通location后正则location,但是"="修饰的普通location需要完全精准匹配,如果没有完全匹配还会继续遍历正则location,上例中的 /index.html 和 /index_notfound.html因为没有完全精准匹配/,所以继续遍历正则location的结果。
练习五、正则location与编辑顺序
-
1、Nginx配置
规则一
server {
listen 8000;
server_name localhost;
location ~ \.html$ {
root html;
deny all;
}
location ~ /prefix/.*\.html$ {
root html;
rewrite /prefix/(.*)\.html /$1.html;
allow all;
}
}
规则二
server {
listen 8000;
server_name localhost;
location ~ /prefix/.*\.html$ {
root html;
allow all;
}
location ~ \.html$ {
root html;
rewrite /prefix/(.*)\.html /$1.html;
deny all;
}
}
- 2、测试结果
URI 请求 | 规则一 HTTP 响应 | 规则二 HTTP 响应 |
---|---|---|
http://localhost:8000/prefix/index.html | 403 Forbidden | 404 Not Found |
http://localhost:8000/index.html | 403 Forbidden | 403 Forbidden |
3、说明
~ /prefix/.*.html$" 表示正则location对于以/prefix/开头.html结尾的所有URI请求都允许访问; location ~.html${deny all;} 表示正则location对于以.html结尾的URI请求都拒绝访问。
正则location的匹配是按照编辑的先后顺序进行匹配,匹配后不再继续遍历剩余的正则location。
练习六、普通location与编辑顺序
- 1、Nginx配置
server {
listen 8000;
server_name localhost;
location /prefix/ {
root html;
allow all;
}
location /prefix/mid/ {
root html;
deny all;
}
}
- 2、测试结果
URI 请求 | HTTP 响应 |
---|---|
http://localhost:8000/prefix/mid/index.html | 403 Forbidden |
http://localhost:8000/prefix/index.html | 404 Not Found |
3、说明
普通location /prefix/mid/匹配会拒绝请求,普通location /prefix/匹配会接受请求。
普通 location 的匹配规则是“最大前缀”匹配,而且与编辑顺序无关。