久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

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

      Nginx 的語(yǔ)法形式是: location [=|~|~*|^~|@] /uri/ { … } ,意思是可以以“ = ”或“ ~* ”或“ ~ ”或“ ^~ ”或“ @ ”符號(hào)為前綴,當(dāng)然也可以沒(méi)有前綴(因?yàn)?nbsp;[A] 是表示可選的 A ; A|B 表示 A 和 B 選一個(gè)),緊接著是 /uri/ ,再接著是{…} 指令塊,整個(gè)意思是對(duì)于滿足這樣條件的 /uri/ 適用指令塊 {…} 的指令。

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

      那么,當(dāng)我們?cè)谝粋€(gè) server 上下文編寫(xiě)了多個(gè) location 的時(shí)候, Nginx 對(duì)于一個(gè) HTTP 請(qǐng)求,是如何匹配到一個(gè) location 做處理呢?用一句話簡(jiǎn)單概括 Nginx 的 location 匹配規(guī)則是:“正則 location ”讓步 “普通 location”的嚴(yán)格精確匹配結(jié)果;但覆蓋 “普通 location ”的最大前綴匹配結(jié)果。理解這句話,我想通過(guò)下面的實(shí)例來(lái)說(shuō)明。

      #1 先普通 location ,再正則 location

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

      例題 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 以“ / ”開(kāi)始的 URI 請(qǐng)求(注意任何 HTTP 請(qǐng)求都必然以“/ ”開(kāi)始,所以“ / ”的意思是所有的請(qǐng)求都能被匹配上),都拒絕訪問(wèn); location ~.html$ {allow all;} 正則 location以 .html 結(jié)尾的 URI 請(qǐng)求,都允許訪問(wèn)。

      測(cè)試結(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 ~]#

      測(cè)試結(jié)果如下:

      URI 請(qǐng)求 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 ”,說(shuō)明被匹配到“ location / {..deny all;} ”了,原因很簡(jiǎn)單HTTP 請(qǐng)求 GET / 被“嚴(yán)格精確”匹配到了普通 location / {} ,則會(huì)停止搜索正則 location ;

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

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

      如果此時(shí)我們?cè)L問(wèn) http://localhost:9090/index.txt 會(huì)是什么結(jié)果呢?顯然是 deny all ;因?yàn)橄绕ヅ渖狭?nbsp;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 的“隱式”嚴(yán)格匹配

      例題 2 :我們?cè)诶} 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;

             }

      }

      測(cè)試請(qǐng)求:

      [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é)果進(jìn)一步驗(yàn)證了“普通 location ”的“嚴(yán)格精確”匹配會(huì)終止對(duì)正則 location 的搜索。這里我們小結(jié)下“普通 location”與“正則 location ”的匹配規(guī)則:先匹配普通 location ,再匹配正則 location ,但是如果普通 location 的匹配結(jié)果恰好是“嚴(yán)格精確( exact match )”的,則 nginx 不再嘗試后面的正則 location ;如果普通 location 的匹配結(jié)果是“最大前綴”,則正則 location 的匹配覆蓋普通 location 的匹配。也就是前面說(shuō)的“正則 location 讓步普通location 的嚴(yán)格精確匹配結(jié)果,但覆蓋普通 location 的最大前綴匹配結(jié)果”。

      #3 普通 location 的“顯式”嚴(yán)格匹配和“ ^~ ” 前綴

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

      例題 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 ^~ / {} ,再看看測(cè)試結(jié)果:

      URI 請(qǐng)求 修改前 修改后
      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 ,原因很簡(jiǎn)單所有請(qǐng)求都是以“ / ”開(kāi)頭,所以所有請(qǐng)求都能匹配上“ / ”普通 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 相對(duì)例題 2 把 location / {} 修改成了 location = / {} ,再次測(cè)試結(jié)果:

      URI 請(qǐng)求 修改前 修改后
      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

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

      #4 正則 location 與編輯順序

      location 的指令與編輯順序無(wú)關(guān),這句話不全對(duì)。對(duì)于普通 location 指令,匹配規(guī)則是:最大前綴匹配(與順序無(wú)關(guān)),如果恰好是嚴(yán)格精確匹配結(jié)果或者加有前綴“ ^~ ”或“ = ”(符號(hào)“ = ”只能?chē)?yán)格匹配,不能前綴匹配),則停止搜索正則 location ;但對(duì)于正則 location 的匹配規(guī)則是:按編輯順序逐個(gè)匹配(與順序有關(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; 

             } 

      }

      測(cè)試結(jié)果:

      URI 請(qǐng)求 配置 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 對(duì)于以 /prefix/ 開(kāi)頭, .html 結(jié)尾的所有 URI 請(qǐng)求,都拒絕訪問(wèn);   location ~.html${allow all;} 表示正則 location 對(duì)于以 .html 結(jié)尾的 URI 請(qǐng)求,都允許訪問(wèn)。 實(shí)際上,prefix 的是 ~.html$ 的子集。

      在“配置 3.1 ”下,兩個(gè)請(qǐng)求都匹配上 location ~.html$ {allow all;} ,并且停止后面的搜索,于是都允許訪問(wèn), 404 Not Found ;在“配置 3.2 ”下, /regextest.html 無(wú)法匹配 prefix ,于是繼續(xù)搜索 ~.html$ ,允許訪問(wèn),于是 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; 

             } 

      }

      測(cè)試結(jié)果:

      URI 請(qǐng)求 配置 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

      測(cè)試結(jié)果表明:普通 location 的匹配規(guī)則是“最大前綴”匹配,而且與編輯順序無(wú)關(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;

             }

      }

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

      測(cè)試一:

      [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 ~]#

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

      直接訪問(wè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 ~]#

      測(cè)試二:訪問(wèn)一個(gè) nginx 不存在,但 baidu 存在的頁(yè)面

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

      顯示,的確百度這個(gè)頁(yè)面是存在的。

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

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

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