linux nginx location,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 permanent;

}

location  = /images {

rewrite /images 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: [following]

--12:05:53--  /

Resolving 61.172.201.194, 61.172.201.195

Connecting to |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 permanent;

}

location  = /images {

rewrite /images 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: [following]

--12:05:53--  /

Resolving 61.172.201.194, 61.172.201.195

Connecting to |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 permanent;

}

location  = /images {

rewrite /images 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: [following]

--12:10:58--  /

Resolving 61.172.201.194, 61.172.201.195

Connecting to |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 ^/(.*)$ permanent;

}

location   = / {

rewrite ^/(.*)$ 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: [following]

--12:31:09--  /

Resolving 61.172.201.195, 61.172.201.194

Connecting to |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 permanent;

}

location  ^~ /images {

rewrite /images 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: [following]

--12:05:53--  /

Resolving 61.172.201.194, 61.172.201.195

Connecting to |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 ^/(.*)$ permanent;

}

location  ^~ /images {

rewrite ^/(.*)$ 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: [following]

--12:10:58--  /

Resolving 61.172.201.194, 61.172.201.195

Connecting to |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 ^/(.*)$ permanent;

}

location  ~ /images {

rewrite ^/(.*)$ 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: [following]

--12:19:17--  /

Resolving 61.172.201.195, 61.172.201.194

Connecting to |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 ^/(.*)$ permanent;

}

location  ~ / {

rewrite ^/(.*)$ 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: [following]

--12:26:26--  /

Resolving 61.172.201.194, 61.172.201.195

Connecting to |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 > /

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27181165/viewspace-777202/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值