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