Nginx配置location规则先级及比较

Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令。Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或正则表达式。但如果要使用正则表达式,则必须指定前缀。

location表达式类型
~ 表示执行一个正则匹配,区分大小写
~* 表示执行一个正则匹配,不区分大小写
^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。
= 进行普通字符精确匹配。也就是完全匹配。
@ “@” 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
location优先级说明

在nginx的location和配置中location的顺序没有太大关系。正location表达式的类型有关。相同类型的表达式,字符串长的会优先匹配。
以下是按优先级排列说明:
第一优先级:等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。
第二优先级:^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
第三优先级:正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。
第四优先级:常规字符串匹配类型。按前缀匹配。

location优先级示例
配置项如下:

location = / {
# 仅仅匹配请求 /
[ configuration A ]
}
location / {
# 匹配所有以 / 开头的请求。但是如果有更长的同类型的表达式,则选择更长的表达式。如果有正则表达式可以匹配,则
# 优先匹配正则表达式。
[ configuration B ]
}
location /documents/ {
# 匹配所有以 /documents/ 开头的请求。但是如果有更长的同类型的表达式,则选择更长的表达式。
#如果有正则表达式可以匹配,则优先匹配正则表达式。
[ configuration C ]
}
location ^~ /images/ {
# 匹配所有以 /images/ 开头的表达式,如果匹配成功,则停止匹配查找。所以,即便有符合的正则表达式location,也
# 不会被使用
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif jpg jpeg结尾的请求。但是 以 /images/开头的请求,将使用 Configuration D
[ configuration E ]
}

请求匹配示例

/ -> configuration A
/index.html -> configuration B
/documents/document.html -> configuration C
/images/1.gif -> configuration D
/documents/1.jpg -> configuration E

注意,以上的匹配和在配置文件中定义的顺序无关。

nginx location规则优先级比较

nginx location中可能涉及的匹配规则有
= 精确匹配
^~ 普通字符匹配,区分大小写
~ 正则匹配,区分大小写
/xxx/yyy.zzz 最长匹配
/
本文所用的nginx版本是
[root@node1 nginx]# nginx -v
nginx version: nginx/1.4.3
实验机器ip为192.168.151.70,浏览器为IE8,不保存cookies。依次对上面的五个匹配规则两两进行对比实验。
1.  =  对比 ^~
        location ^~  /images {
                rewrite ^/images http://www.baidu.com permanent;
        }

        location  = /images {
                rewrite /images http://www.sina.com.cn permanent;
        }
结果
[root@agent3 tmp]# wget http://192.168.151.70/images
--12:05:53--  http://192.168.151.70/images
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:05:53--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn|61.172.201.194|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690283 (674K) [text/html]
Saving to: `index.html'

100%[========================================================================>] 690,283     --.-K/s   in 0.08s  

12:05:54 (8.45 MB/s) - `index.html' saved [690283/690283]
=优先级大于^~

2. = 对比 ~
        location ~  /images {
                rewrite ^/images http://www.baidu.com permanent;
        }

        location  = /images {
                rewrite /images http://www.sina.com.cn permanent;
        }
结果
[root@agent3 tmp]# wget http://192.168.151.70/images
--12:05:53--  http://192.168.151.70/images
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:05:53--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn|61.172.201.194|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690283 (674K) [text/html]
Saving to: `index.html'

100%[========================================================================>] 690,283     --.-K/s   in 0.08s  

12:05:54 (8.45 MB/s) - `index.html' saved [690283/690283]
=优先级大于~

3. = 对比 /xxx/yyy.zzz
        location   /images/images.jsp {
                rewrite ^/images http://www.baidu.com permanent;
        }

        location  = /images {
                rewrite /images http://www.sina.com.cn permanent;
        }
结果:
[root@agent3 tmp]# wget http://192.168.151.70/images/images.jsp
--12:10:58--  http://192.168.151.70/images/images.jsp
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:10:58--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn|61.172.201.194|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690375 (674K) [text/html]
Saving to: `index.html.1'

100%[========================================================================>] 690,375     --.-K/s   in 0.08s  

12:10:58 (8.55 MB/s) - `index.html.1' saved [690375/690375]
=优先级大于/xxx/yyy.zzz

4. = 对比 /
          location   / {
                rewrite ^/(.*)$ http://www.baidu.com permanent;
        }

        location   = / {
                rewrite ^/(.*)$ http://www.sina.com.cn permanent;
        }
结果
[root@agent3 tmp]# wget http://192.168.151.70
--12:31:09--  http://192.168.151.70/
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:31:09--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.195, 61.172.201.194
Connecting to www.sina.com.cn|61.172.201.195|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690538 (674K) [text/html]
Saving to: `index.html.6'

100%[========================================================================>] 690,538     1.93M/s   in 0.3s   

12:31:09 (1.93 MB/s) - `index.html.6' saved [690538/690538]
=优先级大于 /

5 ^~ 对比 ~
        location  ~ /images {
                rewrite /images http://www.baidu.com permanent;
        }

        location  ^~ /images {
                rewrite /images http://www.sina.com.cn permanent;
        }
结果
[root@agent3 tmp]# wget http://192.168.151.70/images
--12:05:53--  http://192.168.151.70/images
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:05:53--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn|61.172.201.194|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690283 (674K) [text/html]
Saving to: `index.html'

100%[========================================================================>] 690,283     --.-K/s   in 0.08s  

12:05:54 (8.45 MB/s) - `index.html' saved [690283/690283]
^~优先级大于~
6. ^~ 对比 /xxx/yyy.zz
        location   /images/images.jsp {
                rewrite ^/(.*)$ http://www.baidu.com permanent;
        }

        location  ^~ /images {
                rewrite ^/(.*)$ http://www.sina.com.cn permanent;
        }
结果
[root@agent3 tmp]# wget http://192.168.151.70/images/images.jsp
--12:10:58--  http://192.168.151.70/images/images.jsp
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:10:58--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn|61.172.201.194|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690375 (674K) [text/html]
Saving to: `index.html.1'

100%[========================================================================>] 690,375     --.-K/s   in 0.08s  
^~优先级大于/xxx/yyy.zzz
12:10:58 (8.55 MB/s) - `index.html.1' saved [690375/690375]

7.  ~ 对比 /xxx/yyy.zzz
        location   /images/images.jsp {
                rewrite ^/(.*)$ http://www.baidu.com permanent;
        }

        location  ~ /images {
                rewrite ^/(.*)$ http://www.sina.com.cn permanent;
        }
结果:
[root@agent3 tmp]# wget http://192.168.151.70/images/images.jsp
--12:19:17--  http://192.168.151.70/images/images.jsp
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:19:17--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.195, 61.172.201.194
Connecting to www.sina.com.cn|61.172.201.195|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690441 (674K) [text/html]
Saving to: `index.html.2'

100%[========================================================================>] 690,441     --.-K/s   in 0.07s  

12:19:17 (9.12 MB/s) - `index.html.2' saved [690441/690441]
~优先级大于/xxx/yyy.zzz

8. ~ 对比 /
        location   / {
                rewrite ^/(.*)$ http://www.baidu.com permanent;
        }

        location  ~ / {
                rewrite ^/(.*)$ http://www.sina.com.cn permanent;
        }
结果
[root@agent3 tmp]# wget http://192.168.151.70
--12:26:26--  http://192.168.151.70/
Connecting to 192.168.151.70:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.sina.com.cn [following]
--12:26:26--  http://www.sina.com.cn/
Resolving www.sina.com.cn... 61.172.201.194, 61.172.201.195
Connecting to www.sina.com.cn|61.172.201.194|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 690516 (674K) [text/html]
Saving to: `index.html.5'

100%[========================================================================>] 690,516     2.23M/s   in 0.3s   

12:26:27 (2.23 MB/s) - `index.html.5' saved [690516/690516]
~优先级大于/

9.  /xxx/yyy.zzz 对比 /
很明显,/xxx/yyy.zzz优先级大于/

总上所述location规则优先级顺序为
= > ^~ > ~ > /xxx/yyy.zzz > /

时间: 2024-09-20 16:50:29

Nginx配置location规则先级及比较的相关文章

nginx 配置 location 总结及 rewrite 规则写法

1. location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到

rewrite规则写法及nginx配置location总结

rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php?id=1&u=str 只对/a/we/index.php重写. 语法: rewrite regex replacement [flag]; 如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用proxy_pass反向代理. 1.location正则写法 一个示例: location =

nginx配置location总结及rewrite规则写法

location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这

【转】Nginx配置location总结及rewrite规则写法

 转载:https://segmentfault.com/a/1190000002797606  http://www.cnblogs.com/lidabo/p/4169396.html nginx 配置文件,自下到上分为三种层次分明的结构: |    http block        the protocol level |    server block        the server level V    location block        the requested URI

nginx配置rewrite规则的例子

一.伪装动态的地址为静态地址 这种是最常用和最简单的,所谓的动态地址即带有参数的URL 如:真实访问地址是http://www.test.com/a.php?b=test 但想通过rewrite实现地址的静态化,方便用户访问想改为http://www.test.com/test.html Apache: RewriteEngine On RewriteRule /test.html /a.php?b=test [L] Nginx: rewrite "/test.html" /a.php

Nginx配置编写时支持逻辑运算与大小写字母转换的方法_nginx

逻辑运算nginx的配置中不支持if条件的逻辑与&& 逻辑或|| 运算 ,而且不支持if的嵌套语法,否则会报下面的错误:nginx: [emerg] invalid condition. 我们可以用变量的方式来间接实现. 要实现的语句: if ($arg_unitid = 42012 && $uri ~/thumb/){ echo "www.jb51.net"; } 如果按照这样来配置,就会报nginx: [emerg] invalid conditio

nginx配置禁止访问或执行目录或文件

nginx配置: location ~ ^/(uploads|images)/.*\.(php|php5|jsp)$ {     deny all; } tips:在目录uploads.images目录下面的所有php.jsp都不能访问. 也有人会选择这样写: location ~ ^/(uploads|images)/.*\.(php|php5|jsp)$ {     return 403; } 这样也是一样的,如果配置了403页面,会跳转过去. 禁止执行脚本 location ~* /(im

Nginx之location 匹配规则详解

关于一些对location认识的误区 1. location 的匹配顺序是"先匹配正则,再匹配普通". 矫正: location 的匹配顺序其实是"先匹配普通,再匹配正则".我这么说,大家一定会反驳我,因为按"先匹配普通,再匹配正则"解释不了大家平时习惯的按"先匹配正则,再匹配普通"的实践经验.这里我只能暂时解释下,造成这种误解的原因是:正则匹配会覆盖普通匹配(实际的规则,比这复杂,后面会详细解释).   2. locatio

Nginx 的 Location 配置指令块

最近一段时间在学习 Nginx ,以前一直对 Nginx 的 Location 配置很头大,最近终于弄出点眉目.总结如下:nginx 配置文件,自下到上分为三种层次分明的结构: |    http block        the protocol level |    server block        the server level V    location block        the requested URI Nginx 允许用户定义 Location block ,并指定