欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          Nginx配置中l(wèi)ocation匹配規(guī)則詳解

          Nginx 的語法形式是: location [=|~|~*|^~|@] /uri/ { … } ,意思是可以以“ = ”或“ ~* ”或“ ~ ”或“ ^~ ”或“ @ ”符號為前綴,當然也可以沒有前綴(因為 [A] 是表示可選的 A ; A|B 表示 A 和 B 選一個),緊接著是 /uri/ ,再接著是{…} 指令塊,整個意思是對于滿足這樣條件的 /uri/ 適用指令塊 {…} 的指令。

          上述各種 location 可分兩大類,分別是:“普通 location ”,官方英文說法是 location using   literal strings 和“正則 location ”,英文說法是 location using regular expressions 。其中“普通 location ”是以“ = ”或“ ^~ ”為前綴或者沒有任何前綴的 /uri/ ;“正則 location ”是以“ ~ ”或“ ~* ”為前綴的 /uri/ 。

          那么,當我們在一個 server 上下文編寫了多個 location 的時候, Nginx 對于一個 HTTP 請求,是如何匹配到一個 location 做處理呢?用一句話簡單概括 Nginx 的 location 匹配規(guī)則是:“正則 location ”讓步 “普通 location”的嚴格精確匹配結(jié)果;但覆蓋 “普通 location ”的最大前綴匹配結(jié)果。理解這句話,我想通過下面的實例來說明。

          #1 先普通 location ,再正則 location

          周邊不少童鞋告訴我, nginx 是“先匹配正則 location 再匹配普通 location ”,其實這是一個誤區(qū), nginx 其實是“先匹配普通 location ,再匹配正則 location ”,但是普通 location 的匹配結(jié)果又分兩種:一種是“嚴格精確匹配”,官方英文說法是“ exact match ”;另一種是“最大前綴匹配”,官方英文說法是“ Literal strings match the beginning portion of the query – the most specific match will be used. ”。我們做個實驗:

          例題 1 :假設(shè) nginx 的配置如下

          server {

                 listen       9090;

                 server_name  localhost;

                            location / {

                     root   html;

                     index  index.html index.htm;

                     deny all;

                 }

                 location ~ .html$ {

                     allow all;

                 }

          }

          附錄 nginx 的目錄結(jié)構(gòu)是: nginx->html->index.html

          上述配置的意思是: location / {… deny all;} 普通 location 以“ / ”開始的 URI 請求(注意任何 HTTP 請求都必然以“/ ”開始,所以“ / ”的意思是所有的請求都能被匹配上),都拒絕訪問; location ~.html$ {allow all;} 正則 location以 .html 結(jié)尾的 URI 請求,都允許訪問。

          測試結(jié)果:

          [root@web108 ~]# curl http://localhost:9090/

          <html>

          <head><title>403 Forbidden</title></head>

          <body bgcolor=”white”>

          <center><h1>403 Forbidden</h1></center>

          <hr><center>nginx/1.1.0</center>

          </body>

          </html>

          [root@web108 ~]# curl http://localhost:9090/index.html

          <html>

          <head>

          <title>Welcome to nginx!</title>

          </head>

          <body bgcolor=”white” text=”black”>

          <center><h1>Welcome to nginx!</h1></center>

          </body>

          </html>

          [root@web108 ~]# curl http://localhost:9090/index_notfound.html

          <html>

          <head><title>404 Not Found</title></head>

          <body bgcolor=”white”>

          <center><h1>404 Not Found</h1></center>

          <hr><center>nginx/1.1.0</center>

          </body>

          </html>

          [root@web108 ~]#

          測試結(jié)果如下:

          URI 請求 HTTP 響應(yīng)
          curl http://localhost:9090/ 403 Forbidden
          curl http://localhost:9090/index.html Welcome to nginx!
          curl http://localhost:9090/index_notfound.html 404 Not Found

          curl http://localhost:9090/ 的結(jié)果是“ 403 Forbidden ”,說明被匹配到“ location / {..deny all;} ”了,原因很簡單HTTP 請求 GET / 被“嚴格精確”匹配到了普通 location / {} ,則會停止搜索正則 location ;

          curl http://localhost:9090/index.html 結(jié)果是“ Welcome to nginx! ”,說明沒有被“ location / {…deny all;} ”匹配,否則會 403 Forbidden ,但 /index.html 的確也是以“ / ”開頭的,只不過此時的普通 location / 的匹配結(jié)果是“最大前綴”匹配,所以 Nginx 會繼續(xù)搜索正則 location , location ~ .html$ 表達了以 .html 結(jié)尾的都 allow all; 于是接著就訪問到了實際存在的 index.html 頁面。

          curl http://localhost:9090/index_notfound.html   同樣的道理先匹配 location / {} ,但屬于“普通 location 的最大前綴匹配”,于是后面被“正則 location ” location ~ .html$ {} 覆蓋了,最終 allow all ; 但的確目錄下不存在index_notfound.html 頁面,于是 404 Not Found 。

          如果此時我們訪問 http://localhost:9090/index.txt 會是什么結(jié)果呢?顯然是 deny all ;因為先匹配上了 location / {..deny all;} 盡管屬于“普通 location ”的最大前綴匹配結(jié)果,繼續(xù)搜索正則 location ,但是 /index.txt 不是以 .html結(jié)尾的,正則 location 失敗,最終采納普通 location 的最大前綴匹配結(jié)果,于是 deny all 了。

          [root@web108 ~]# curl http://localhost:9090/index.txt

          <html>

          <head><title>403 Forbidden</title></head>

          <body bgcolor=”white”>

          <center><h1>403 Forbidden</h1></center>

          <hr><center>nginx/1.1.0</center>

          </body>

          </html>

          [root@web108 ~]#

          #2 普通 location 的“隱式”嚴格匹配

          例題 2 :我們在例題 1 的基礎(chǔ)上增加精確配置

          server {

                 listen       9090;

                 server_name  localhost;

                            location /exact/match.html {

                     allow all;

                 }

                            location / {

                     root   html;

                     index  index.html index.htm;

                     deny all;

                 }

                 location ~ .html$ {

                     allow all;

                 }

          }

          測試請求:

          [root@web108 ~]# curl http://localhost:9090/exact/match.html

          <html>

          <head><title>404 Not Found</title></head>

          <body bgcolor=”white”>

          <center><h1>404 Not Found</h1></center>

          <hr><center>nginx/1.1.0</center>

          </body>

          </html>

          [root@web108 ~]#

          結(jié)果進一步驗證了“普通 location ”的“嚴格精確”匹配會終止對正則 location 的搜索。這里我們小結(jié)下“普通 location”與“正則 location ”的匹配規(guī)則:先匹配普通 location ,再匹配正則 location ,但是如果普通 location 的匹配結(jié)果恰好是“嚴格精確( exact match )”的,則 nginx 不再嘗試后面的正則 location ;如果普通 location 的匹配結(jié)果是“最大前綴”,則正則 location 的匹配覆蓋普通 location 的匹配。也就是前面說的“正則 location 讓步普通location 的嚴格精確匹配結(jié)果,但覆蓋普通 location 的最大前綴匹配結(jié)果”。

          #3 普通 location 的“顯式”嚴格匹配和“ ^~ ” 前綴

          上面我們演示的普通 location 都是不加任何前綴的,其實普通 location 也可以加前綴:“ ^~ ”和“ = ”。其中“ ^~”的意思是“非正則,不需要繼續(xù)正則匹配”,也就是通常我們的普通 location ,還會繼續(xù)搜索正則 location (恰好嚴格精確匹配除外),但是 nginx 很人性化允許配置人員告訴 nginx 某條普通 location ,無論最大前綴匹配,還是嚴格精確匹配都終止繼續(xù)搜索正則 location ;而“ = ”則表達的是普通 location 不允許“最大前綴”匹配結(jié)果,必須嚴格等于,嚴格精確匹配。

          例題 3 :“ ^~ ”前綴的使用

          server {

                 listen       9090;

                 server_name  localhost;

                            location /exact/match.html {

                     allow all;

                 }

                           location ^~ / {

                     root   html;

                     index  index.html index.htm;

                     deny all;

                 }

                 location ~ .html$ {

                     allow all;

                 }

          }

          把例題 2 中的 location / {} 修改成 location ^~ / {} ,再看看測試結(jié)果:

          URI 請求 修改前 修改后
          curl http://localhost:9090/ 403 Forbidden 403 Forbidden
          curl http://localhost:9090/index.html Welcome to nginx! 403 Forbidden
          curl http://localhost:9090/index_notfound.html 404 Not Found 403 Forbidden
          curl http://localhost:9090/exact/match.html 404 Not Found 404 Not Found

          除了 GET /exact/match.html 是 404 Not Found ,其余都是 403 Forbidden ,原因很簡單所有請求都是以“ / ”開頭,所以所有請求都能匹配上“ / ”普通 location ,但普通 location 的匹配原則是“最大前綴”,所以只有/exact/match.html 匹配到 location /exact/match.html {allow all;} ,其余都 location ^~ / {deny all;} 并終止正則搜索。

          例題 4 :“ = ”前綴的使用

          server {

                 listen       9090;

                 server_name  localhost;

                            location /exact/match.html {

                     allow all;

                 }

                           location = / {

                     root   html;

                     index  index.html index.htm;

                     deny all;

                 }

                 location ~ .html$ {

                     allow all;

                 }

          }

          例題 4 相對例題 2 把 location / {} 修改成了 location = / {} ,再次測試結(jié)果:

          URI 請求 修改前 修改后
          curl http://localhost:9090/ 403 Forbidden 403 Forbidden
          curl http://localhost:9090/index.html Welcome to nginx! Welcome to nginx!
          curl http://localhost:9090/index_notfound.html 404 Not Found 404 Not Found
          curl http://localhost:9090/exact/match.html 404 Not Found 404 Not Found
          curl http://localhost:9090/test.jsp 403 Forbidden 404 Not Found

          最能說明問題的測試是 GET /test.jsp ,實際上 /test.jsp 沒有匹配正則 location ( location ~.html$ ),也沒有匹配 location = / {} ,如果按照 location / {} 的話,會“最大前綴”匹配到普通 location / {} ,結(jié)果是 deny all 。

          #4 正則 location 與編輯順序

          location 的指令與編輯順序無關(guān),這句話不全對。對于普通 location 指令,匹配規(guī)則是:最大前綴匹配(與順序無關(guān)),如果恰好是嚴格精確匹配結(jié)果或者加有前綴“ ^~ ”或“ = ”(符號“ = ”只能嚴格匹配,不能前綴匹配),則停止搜索正則 location ;但對于正則 location 的匹配規(guī)則是:按編輯順序逐個匹配(與順序有關(guān)),只要匹配上,就立即停止后面的搜索。

          配置 3.1

          server {

                 listen       9090;

                 server_name  localhost;

                 location ~ .html$ {

                     allow all; 

                 } 

                 location ~ ^/prefix/.*.html$ {

                     deny all; 

                 } 

          }

          配置 3.2

          server {

                 listen       9090;

                 server_name  localhost;

               

                 location ~ ^/prefix/.*.html$ {

                     deny all; 

                 } 

                           

                            location ~ .html$ {

                     allow all; 

                 } 

          }

          測試結(jié)果:

          URI 請求 配置 3.1 配置 3.2
          curl http://localhost:9090/regextest.html 404 Not Found 404 Not Found
          curl http://localhost:9090/prefix/regextest.html 404 Not Found 403 Forbidden

          解釋:

          Location ~ ^/prefix/.*.html$ {deny all;} 表示正則 location 對于以 /prefix/ 開頭, .html 結(jié)尾的所有 URI 請求,都拒絕訪問;   location ~.html${allow all;} 表示正則 location 對于以 .html 結(jié)尾的 URI 請求,都允許訪問。 實際上,prefix 的是 ~.html$ 的子集。

          在“配置 3.1 ”下,兩個請求都匹配上 location ~.html$ {allow all;} ,并且停止后面的搜索,于是都允許訪問, 404 Not Found ;在“配置 3.2 ”下, /regextest.html 無法匹配 prefix ,于是繼續(xù)搜索 ~.html$ ,允許訪問,于是 404 Not Found ;然而 /prefix/regextest.html 匹配到 prefix ,于是 deny all , 403 Forbidden 。

          配置 3.3

          server {

                 listen       9090;

                 server_name  localhost;

                 location  /prefix/ {

                         deny all; 

                 } 

                   

                 location  /prefix/mid/ {

                         allow all; 

                 } 

          }

          配置 3.4

          server {

                 listen       9090;

                 server_name  localhost;

               

                 location  /prefix/mid/ {

                         allow all; 

                 } 

                            location  /prefix/ {

                         deny all; 

                 } 

          }

          測試結(jié)果:

          URI 請求 配置 3.1 配置 3.2
          curl http://localhost:9090/prefix/t.html 403 Forbidden 403 Forbidden
          curl http://localhost:9090/prefix/mid/t.html 404 Not Found 404 Not Found

          測試結(jié)果表明:普通 location 的匹配規(guī)則是“最大前綴”匹配,而且與編輯順序無關(guān)。

          #5 “@” 前綴 Named Location 使用

          REFER:  http://wiki.nginx.org/HttpCoreModule#error_page

          假設(shè)配置如下:

          server {

                 listen       9090;

                 server_name  localhost;

                  location  / {

                     root   html;

                     index  index.html index.htm;

                     allow all;

                 }

                 #error_page 404 http://www.baidu.com # 直接這樣是不允許的

                 error_page 404 = @fallback;

                 location @fallback {

                     proxy_pass http://www.baidu.com;

                 }

          }

          上述配置文件的意思是:如果請求的 URI 存在,則本 nginx 返回對應(yīng)的頁面;如果不存在,則把請求代理到baidu.com 上去做個彌補(注: nginx 當發(fā)現(xiàn) URI 對應(yīng)的頁面不存在, HTTP_StatusCode 會是 404 ,此時error_page 404 指令能捕獲它)。

          測試一:

          [root@web108 ~]# curl http://localhost:9090/nofound.html -i

          HTTP/1.1 302 Found

          Server: nginx/1.1.0

          Date: Sat, 06 Aug 2011 08:17:21 GMT

          Content-Type: text/html; charset=iso-8859-1

          Location: http://localhost:9090/search/error.html

          Connection: keep-alive

          Cache-Control: max-age=86400

          Expires: Sun, 07 Aug 2011 08:17:21 GMT

          Content-Length: 222

          <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>

          <html><head>

          <title>302 Found</title>

          </head><body>

          <h1>Found</h1>

          <p>The document has moved <a href=”http://www.baidu.com/search/error.html”>here</a>.</p>

          </body></html>

          [root@web108 ~]#

          當我們 GET /nofound.html 發(fā)送給本 nginx , nginx 找不到對應(yīng)的頁面,于是 error_page 404 = @fallback ,請求被代理到 http://www.baidu.com ,于是 nginx 給 http://www.baidu.com 發(fā)送了 GET /nofound.html ,但/nofound.html 頁面在百度也不存在,百度 302 跳轉(zhuǎn)到錯誤頁。

          直接訪問 http://www.baidu.com/nofound.html 結(jié)果:

          [root@web108 ~]# curl http://www.baidu.com/nofound.html -i

          HTTP/1.1 302 Found

          Date: Sat, 06 Aug 2011 08:20:05 GMT

          Server: Apache

          Location: http://www.baidu.com/search/error.html

          Cache-Control: max-age=86400

          Expires: Sun, 07 Aug 2011 08:20:05 GMT

          Content-Length: 222

          Connection: Keep-Alive

          Content-Type: text/html; charset=iso-8859-1

          <!DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”>

          <html><head>

          <title>302 Found</title>

          </head><body>

          <h1>Found</h1>

          <p>The document has moved <a href=”http://www.baidu.com/search/error.html”>here</a>.</p>

          </body></html>

          [root@web108 ~]#

          測試二:訪問一個 nginx 不存在,但 baidu 存在的頁面

          [root@web108 ~]# curl http://www.baidu.com/duty/ -i

          HTTP/1.1 200 OK

          Date: Sat, 06 Aug 2011 08:21:56 GMT

          Server: Apache

          P3P: CP=” OTI DSP COR IVA OUR IND COM ”

          P3P: CP=” OTI DSP COR IVA OUR IND COM ”

          Set-Cookie: BAIDUID=5C5D2B2FD083737A0C88CA7075A6601A:FG=1; expires=Sun, 05-Aug-12 08:21:56 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

          Set-Cookie: BAIDUID=5C5D2B2FD083737A2337F78F909CCB90:FG=1; expires=Sun, 05-Aug-12 08:21:56 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

          Last-Modified: Wed, 05 Jan 2011 06:44:53 GMT

          ETag: “d66-49913b8efe340″

          Accept-Ranges: bytes

          Content-Length: 3430

          Cache-Control: max-age=86400

          Expires: Sun, 07 Aug 2011 08:21:56 GMT

          Vary: Accept-Encoding,User-Agent

          Connection: Keep-Alive

          Content-Type: text/html

          <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

          “http://www.w3.org/TR/html4/loose.dtd”>

          。。。。

          </body>

          </html>

          顯示,的確百度這個頁面是存在的。

          [root@web108 ~]# curl http://localhost:9090/duty/ -i

          HTTP/1.1 200 OK

          Server: nginx/1.1.0

          Date: Sat, 06 Aug 2011 08:23:23 GMT

          Content-Type: text/html

          Connection: keep-alive

          P3P: CP=” OTI DSP COR IVA OUR IND COM ”

          P3P: CP=” OTI DSP COR IVA OUR IND COM ”

          Set-Cookie: BAIDUID=8FEF0A3A2C31D277DCB4CC5F80B7F457:FG=1; expires=Sun, 05-Aug-12 08:23:23 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

          Set-Cookie: BAIDUID=8FEF0A3A2C31D277B1F87691AFFD7440:FG=1; expires=Sun, 05-Aug-12 08:23:23 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

          Last-Modified: Wed, 05 Jan 2011 06:44:53 GMT

          ETag: “d66-49913b8efe340″

          Accept-Ranges: bytes

          Content-Length: 3430

          Cache-Control: max-age=86400

          Expires: Sun, 07 Aug 2011 08:23:23 GMT

          Vary: Accept-Encoding,User-Agent

          <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

          “http://www.w3.org/TR/html4/loose.dtd”>

          <html>

          。。。

          </body>

          </html>

          當 curl http://localhost:9090/duty/ -i 時, nginx 沒找到對應(yīng)的頁面,于是 error_page = @fallback ,把請求代理到 baidu.com 。注意這里的 error_page = @fallback 不是靠重定向?qū)崿F(xiàn)的,而是所說的“ internally redirected (forward )”。

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號