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 ^~ /p_w_picpaths {
rewrite ^/p_w_picpaths http://www.baidu.com permanent;
}
location = /p_w_picpaths {
rewrite /p_w_picpaths http://www.sina.com.cn permanent;
}
结果
[root@agent3 tmp]# wget http://192.168.151.70/p_w_picpaths
--12:05:53-- http://192.168.151.70/p_w_picpaths
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 ~ /p_w_picpaths {
rewrite ^/p_w_picpaths http://www.baidu.com permanent;
}
location = /p_w_picpaths {
rewrite /p_w_picpaths http://www.sina.com.cn permanent;
}
结果
[root@agent3 tmp]# wget http://192.168.151.70/p_w_picpaths
--12:05:53-- http://192.168.151.70/p_w_picpaths
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 /p_w_picpaths/p_w_picpaths.jsp {
rewrite ^/p_w_picpaths http://www.baidu.com permanent;
}
location = /p_w_picpaths {
rewrite /p_w_picpaths http://www.sina.com.cn permanent;
}
结果:
[root@agent3 tmp]# wget http://192.168.151.70/p_w_picpaths/p_w_picpaths.jsp
--12:10:58-- http://192.168.151.70/p_w_picpaths/p_w_picpaths.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 ~ /p_w_picpaths {
rewrite /p_w_picpaths http://www.baidu.com permanent;
}
location ^~ /p_w_picpaths {
rewrite /p_w_picpaths http://www.sina.com.cn permanent;
}
结果
[root@agent3 tmp]# wget http://192.168.151.70/p_w_picpaths
--12:05:53-- http://192.168.151.70/p_w_picpaths
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 /p_w_picpaths/p_w_picpaths.jsp {
rewrite ^/(.*)$ http://www.baidu.com permanent;
}
location ^~ /p_w_picpaths {
rewrite ^/(.*)$ http://www.sina.com.cn permanent;
}
结果
[root@agent3 tmp]# wget http://192.168.151.70/p_w_picpaths/p_w_picpaths.jsp
--12:10:58-- http://192.168.151.70/p_w_picpaths/p_w_picpaths.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 /p_w_picpaths/p_w_picpaths.jsp {
rewrite ^/(.*)$ http://www.baidu.com permanent;
}
location ~ /p_w_picpaths {
rewrite ^/(.*)$ http://www.sina.com.cn permanent;
}
结果:
[root@agent3 tmp]# wget http://192.168.151.70/p_w_picpaths/p_w_picpaths.jsp
--12:19:17-- http://192.168.151.70/p_w_picpaths/p_w_picpaths.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 > /
nginx location规则优先级比较
转载文章标签 nginx location规则优先级 文章分类 Nginx 服务器
-
Nginx location 配置的优先级
Nginx location 配置的优先级1、表达式的几种形式~ 表示执行一个正则匹配,区分大小写~* 表示执行一
nginx location 优先级 正则匹配 配置文件 -
nginx_location匹配规则说明以及匹配的优先级
关于nginx中locaiton的匹配规则和匹配顺序
匹配规则 匹配顺序 location -
Nginx关于location 的匹配规则详解,Nginx location 匹配优先级,Nginx的location匹配规则和全局变量
概述Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文
Nginx关于location 的匹配规 Nginx location 匹配优先级 Nginx的location匹配规则和全