1. 程式人生 > >nginx location規則優先順序比較

nginx location規則優先順序比較

nginx location中可能涉及的匹配規則有
= 精確匹配
^~ 普通字元匹配,區分大小寫
~ 正則匹配,區分大小寫
/xxx/yyy.zzz 最長匹配
/
本文所用的nginx版本是
[[email protected] 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;
        }
結果
[
[email protected]
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;
        }
結果
[
[email protected]
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;
        }
結果:
[
[email protected]
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;
        }
結果
[[email protected] 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;
        }
結果
[[email protected] 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;
        }
結果
[[email protected] 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;
        }
結果:
[[email protected] 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;
        }
結果
[[email protected] 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中可能涉及的匹配規則有 = 精確匹配 ^~ 普通字元匹配,區分大小寫 ~ 正則匹配,區分大小寫 /xxx/yyy.zzz 最長匹配 / 本文所用的nginx版本是 [[email protected] nginx]# nginx -v

nginx location優先順序

原來一直以為location的優先順序是先後順序,結果有次專案中傻眼了,趕緊百度一下,下面的內容參考了這個連結 location表示式型別 ~ 表示執行一個正則匹配,區分大小寫~* 表示執行一個正則匹配,不區分大小寫^~ 表示普通字元匹配。使用字首匹配。如果匹配成功,則不再匹配其他location。= 進

nginx location匹配規則

nginx location匹配規則location匹配命令~ #波浪線表示執行一個正則匹配,區分大小寫 ~* #表示執行一個正則匹配,不區分大小寫 ^~ #^~表示普通字符匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,一般用來匹配目錄 = #進行普通字符精確匹配 @

nginx location if 的匹配規則

nginx location if 的匹配規則 cuizhiliang關注0人評論 23197人閱讀 2016-11-20 16:13:32   cation匹配命令 ~ &

[整理] Nginx Location 匹配規則

目錄 規則語法 location 分類 匹配順序: 擴充套件 location / {}和 location =/ {}的區別 如何快速測試 規則語法 語法 匹配規則 空 普通匹配

Nginx location指令匹配順序規則

gin 順序 過程 ring 舉例 請求 ret 其他 html location匹配命令 1. “= ”,字面精確匹配, 如果匹配,則跳出匹配過程。(不再進行正則匹配) 2. “^~ ”,最大前綴匹配,如果匹配,則跳出匹配過程。(不再進行正則匹配) 3. 不帶任何

nginx location 匹配規則

原文:https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx/nginx_local_pcre.html 語法規則 location [=|~|~*|^~] /uri/ { … } 符號

Nginx Location指令URI匹配規則詳解

1、介紹 location指令是http模組當中最核心的一項配置,根據預先定義的URL匹配規則來接收使用者傳送的請求,根據匹配結果,將請求轉發到後臺伺服器、非法的請求直接拒絕並返回403、404、500錯誤處理等。 2、location指令語法 l

nginx location正則匹配規則

一個示例: location = / { # 精確匹配 / ,主機名後面不能帶任何字串 [ configuration A ] } location / { # 因為所有的地址都以 / 開頭,所以這條規則將匹配到所有請求 # 但是正則和最長字

Nginx Location配置總結

login htm dex val edi 網站 通用 返回 web 語法規則: location [=|~|~*|^~] /uri/ { … } = 開頭表示精確匹配^~ 開頭表示uri以某個常規字符串開頭,理解為匹配 url路徑即可。nginx不對url做編碼,因此請求

Nginx Location和Rewrite深入剖析

nginx rewrite location Nginx Location和Rewrite深入剖析Nginx LocationNginx由內核和模塊組成,其中內核的設計非常微小和簡潔,完成的工作也非常簡單,僅僅通過查找配置文件將客戶端的請求映射到一個location block,而location

Laravel的Nginx重寫規則完整代碼

spa nginx錯誤 action cgi code file pre req permanent laravel基本重寫規則 location / { index index.html index.htm index.php;

nginx重寫規則配置

str 特殊 完成 官網 log byte reg $1 主機頭 https://segmentfault.com/a/1190000002797606 location正則寫法 一個示例: location = / { # 精確匹配 / ,主機名後面不能帶任何字

nginx location語法使用說明

狀態 title valid 存在 語法規則 字符串 server location bsp 語法規則: location [=|~|~*|^~] /uri/ { … } = 開頭表示精確匹配 ^~ 開頭表示uri以某個常規字符串開頭,理解為匹配 url

nginx location 語法

意思 當前 需要 bsp 如果 處理請求 僅供參考 參數 url location 語法location 有”定位”的意思, 根據Uri來進行不同的定位.在虛擬主機的配置中,是必不可少的,location可以把網站的不同部分,定位到不同的處理方式上.比如, 碰到.php,

nginx location詳解

後綴 fig 案例 css 部分 一個 匹配 字符 寫法 Location block 的基本語法形式是: location [=|~|~*|^~|@] pattern { ... } [=|~|~*|^~|@] 被稱作 location modifier ,這會定

Nginx Rewrite規則詳解

位置 定向 支持 rman 需求 clas 域名 匹配 .com Rewrite規則含義就是某個URL重寫成特定的URL,從某種意義上說為了美觀或者對搜索引擎友好,提高收錄量及排名等。 Rewrite規則的最後一項參數為flag標記,支持的flag標記主要有以下幾種: 1

nginx location

uri .com 完全匹配 .html spa 但是 class 完全 b- 匹配模式及順序   location = /uri    =開頭表示精確匹配,只有完全匹配上才能生效。   location ^~ /uri   ^~ 開頭對URL路徑進行前綴匹配,並且在正則之前

[nginx]location語法

正則表達 turn 字段 順序 html root 精確 cnblogs 默認 location語法 location語法格式 location [=|~|~*|^~] uri { .... } location [=|~|~*|^~] uri

Nginx location 正則篇

nbsp cat 正則 區分大小寫 gin 繼續 pos alias 如果 location 前綴 沒有前綴 匹配以指定模式開頭的location = 精準匹配,不是以指定模式開頭 ~