Nginx 指令之break和last


參考自:https://www.cnblogs.com/xuliangwei/p/11568223.html

兩個指令用法相同,但含義不同,需要放到rewrite規則的末尾,用來控制重寫后的URL或uri是否繼續被h后面的nginx配置執行(主要是rewrite、return指令)。

break 與 last 的區別

     last: 停止當前這個請求,並根據rewrite匹配的規則重新發起一個請求。新請求又從第一階段開始執行…
  break:相對last,break並不會重新發起一個請求,只是跳過當前的rewrite階段,並執行本請求后續的執行階段…

 

示例說明

1、連續兩條rewrite規則

server{
    listen 80; 
    server_name test.com;
    root html;

    rewrite /1.html /2.html ;
    rewrite /2.html /3.html ;
    
}
View Code
當我們請求1.html時,最終訪問到的是3.html,兩條rewrite規則先后執行。

2、break和last 在location {} 外部

server 中沒有location

server{
    listen 80; 
    server_name test.com;
    root html;

    rewrite /1.html /2.html break;
    rewrite /2.html /3.html;
}
View Code
當我們請求1.html時,最終訪問到的是2.html,說明break在此示例中,作用是不再執行break以下的rewrite規則。注意即使是在同一location內部這種情況也不會執行同一location內部break后面的rewrite等指令。至於不同location 會不會執行下面有說明。

break或last后面還有location時

server{
    listen 80; 
    server_name test.com;
    root html;

    rewrite /1.html /2.html;
    break;
    rewrite /2.html /3.html;     #不執行此處 
   
    location /2.html {              #匹配此處,符合的話執行{}里面配置
        return 403;
    }
}
View Code
當請求1.html時,最終會返回403狀態碼,說明它去匹配了break后面的location{}配置,(前提是請求要匹配該location)。注意上述兩個示例對於last 也是相同的情況。

3、當break在location{}里面

多個rewrite 沒有break

server{
    listen 80; 
    server_name test.com;
    root html;
    
    location / {
        rewrite /1.html /2.html;
        rewrite /2.html /3.html;
    }
    location /2.html
    {
        rewrite /2.html /a.html;
    }
    location /3.html
    {
        rewrite /3.html /b.html;
    }
}
View Code

當請求/1.html,最終將會訪問/b.html,連續執行location /下的兩次rewrite,跳轉到了/3.html,然后又匹配location /3.html 

 

location 里面有break

server{
    listen 80; 
    server_name test.com;
    root html;
    
    location / {
        rewrite /1.html /2.html;
        break;
        rewrite /2.html /3.html;
    }
    location /2.html
    {
        rewrite /2.html /a.html;
    }
    location /3.html
    {
        rewrite /3.html /b.html;
    }
}
View Code
當請求/1.html,最終會訪問/2.html.在location{}內部,遇到break,本location{}內以及后面的所有location{}內的所有指令都不再執行,注意是不在匹配后面的locatin ,以及不再執行本location里面break后面的rewrite 或 return 等指令,但是如果后面是proxy_pass配置就會執行代理轉發。

4、last 在location里面

server{
    listen 80; 
    server_name test.com;
    root html;
    
    location / {
        rewrite /1.html /2.html last;
        rewrite /2.html /3.html;        #此處跳過不執行
    }
    location /2.html
    {
        rewrite /2.html /a.html;
    }
    location /3.html
    {
        rewrite /3.html /b.html;
    }
}
View Code
當請求/1.html,最終會訪問/a.html。在location{}內部,遇到last,本location{}內后續指令不再執行,而重寫后的url再次從頭開始,從頭到尾匹配一遍規則。

總結

1、當rewrite規則在location{}外,break和last作用一樣,遇到break或last后,其后續的rewrite/return語句不再執行。但后續有location{}的話,還會近一步執行location{}里面的語句,前提是請求必須要匹配該location。

2、當rewrite規則在location{}里,遇到break后,本location{}與其他location{}的所有rewrite/return規則都不再執行,只會執行本location中的root 或者proxypass。

3、當rewrite規則在location{}里,遇到last后,本location{}里后續rewrite/return規則不執行,但重寫后的url再次從頭開始執行所有規則,哪個匹配執行哪個。

4、break 與 last 的區別:

     last: 停止當前這個請求,並根據rewrite匹配的規則重新發起一個請求。新請求又從第一階段開始執行…
  break:相對last,break並不會重新發起一個請求,只是跳過當前的rewrite階段,並執行本請求后續的執行階段…

5、配置rewrite last時,請求跳出當前location,進入server塊,重新進行location匹配,超過10次匹配不到報500錯誤。客戶端的url不變。(未驗證過)

注意

rewrite  的執行不會影響瀏覽器中的請求的url ,現實的還是原來的地址,日志中顯示的uri 也是重寫之前的uri。

如果rewrite uri 后進行的是proxy_pass 轉發,轉到另一個nginx 服務器后,該nginx 日志記錄的是重寫后的uri。

沒有break的rewrite 可能會產生死循環,因為請求被rewrite 后會新的URL重新向下去匹配規則,如果沒有符合的會繼續從頭開始。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM