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 > /
= 精確匹配
^~ 普通字元匹配,區分大小寫
~ 正則匹配,區分大小寫
/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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Nginx的location規則:優先順序和路徑替換Nginx
- 談Nginx的Location匹配優先順序Nginx
- nginx的location匹配順序、優先順序,location對映衝突排查Nginx
- Nginx location 在配置中的優先順序Nginx
- CSS 選擇器優先順序規則CSS
- Nginx配置指令location匹配符優先順序和安全問題Nginx
- nginx快取優先順序Nginx快取
- 理解C語言宣告的優先順序規則C語言
- nginx location匹配規則Nginx
- CSS優先順序CSS
- nginx location匹配及rewrite規則Nginx
- Nginx的location配置規則梳理Nginx
- 中斷優先順序
- nginx的location 規則匹配練習Nginx
- python運算子及優先順序順序Python
- 高效能Web伺服器Nginx的配置與部署研究(16)小議location匹配模式優先順序Web伺服器Nginx模式
- Android程式優先順序Android
- SQL 優先順序join>whereSQL
- java運算子優先順序Java
- php運算子優先順序PHP
- css優先順序彙總CSS
- java setPriority()設定優先順序Java
- [譯]HTTP/2的優先順序HTTP
- 封裝優先順序佇列封裝佇列
- Yarn任務優先順序配置Yarn
- gitignore優先順序小結Git
- css 選擇器優先順序CSS
- CSS的處理優先順序CSS
- java執行緒優先順序Java執行緒
- 資料型別優先順序資料型別
- 深入理解css優先順序CSS
- NLS引數優先順序解析
- 華為路由協議優先順序路由協議
- C++運算子優先順序C++
- SpringBoot配置檔案優先順序載入順序Spring Boot
- CSS規則的執行順序CSS
- nginx配置location總結及rewrite規則寫法Nginx
- 何為CSS 樣式優先順序CSS