nginx模塊

if指令 格式:html

if (條件判斷) { 具體的rewrite規則 }nginx

條件舉例正則表達式

條件判斷語句由Nginx內置變量、邏輯判斷符號和目標字符串三部分組成。
其中,內置變量是Nginx固定的非自定義的變量,如,$request_method, $request_uri等。
邏輯判斷符號,有=, !=, ~, ~*, !~, !~*
!表示相反的意思,~爲匹配符號,它右側爲正則表達式,區分大小寫,而~*爲不區分大小寫匹配。
目標字符串能夠是正則表達式,一般不用加引號,但表達式中有特殊符號時,好比空格、花括號、分號等,須要用單引號引發來。

示例1ide

if ($request_method = POST)  //當請求的方法爲POST時,直接返回405狀態碼
{
    return 405; //在該示例中並未用到rewrite規則,if中支持用return指令。
}


示例2

if ($http_user_agent ~ MSIE) //user_agent帶有MSIE字符的請求,直接返回403狀態碼 { return 403; }url

若是想同時限制多個user_agent,還能夠寫成這樣spa

if ($http_user_agent ~ "MSIE|firefox|spider") { return 403; }firefox

示例3

if(!-f $request_filename) //當請求的文件不存在,將會執行下面的rewrite規則 { rewrite 語句; }code

示例4

if($request_uri ~* 'gid=\d{9,12}/') //\d表示數字,{9,12}表示數字出現的次數是9到12次,如gid=123456789/就是符合條件的。 { rewrite 語句; }server

rewrite中的break和last

兩個指令用法相同,但含義不一樣,須要放到rewrite規則的末尾,用來控制重寫後的連接是否繼續被nginx配置執行(主要是rewrite、return指令)。

示例1(連續兩條rewrite規則):
server{
    listen 80; 
    server_name test.com;
    root /tmp/123.com;

    rewrite /1.html /2.html ;
    rewrite /2.html /3.html ;
    
}
當咱們請求1.html時,最終訪問到的是3.html,兩條rewrite規則前後執行。

break和last在location {}外部htm

格式:rewrite xxxxx  break;

示例2(增長break):
server{
    listen 80; 
    server_name test.com;
    root /tmp/123.com;

    rewrite /1.html /2.html break;
    rewrite /2.html /3.html;
}
當咱們請求1.html時,最終訪問到的是2.html
說明break在此示例中,做用是再也不執行break如下的rewrite規則。

但,當配置文件中有location時,它還會去執行location{}段的配置(請求要匹配該location)。

示例3(break後面還有location段):
server{
    listen 80; 
    server_name test.com;
    root /tmp/123.com;

    rewrite /1.html /2.html break;
    rewrite /2.html /3.html;
    location /2.html {
        return 403;
    }
}
當請求1.html時,最終會返回403狀態碼,說明它去匹配了break後面的location{}配置。

以上2個示例中,能夠把break替換爲last,它們二者起到的效果如出一轍。

當break和last在location{}裏面

示例4(什麼都不加):
server{
    listen 80; 
    server_name test.com;
    root /tmp/123.com;
    
    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;
    }
}
當請求/1.html,最終將會訪問/b.html,連續執行location /下的兩次rewrite,跳轉到了/3.html,而後又匹配location /3.html

示例5(增長break):
server{
    listen 80; 
    server_name test.com;
    root /tmp/123.com;
    
    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;
    }
}
當請求/1.html,最終會訪問/2.html
在location{}內部,遇到break,本location{}內以及後面的全部location{}內的全部指令都再也不執行。


示例6(增長last):
server{
    listen 80; 
    server_name test.com;
    root /tmp/123.com;
    
    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;
    }
}
當請求/1.html,最終會訪問/a.html
在location{}內部,遇到last,本location{}內後續指令再也不執行,而重寫後的url再次從頭開始,從頭至尾匹配一遍規則。

結論

  • 當rewrite規則在location{}外,break和last做用同樣,遇到break或last後,其後續的rewrite/return語句再也不執行。但後續有location{}的話,還會近一步執行location{}裏面的語句,固然前提是請求必需要匹配該location。
  • 當rewrite規則在location{}裏,遇到break後,本location{}與其餘location{}的全部rewrite/return規則都再也不執行。
  • 當rewrite規則在location{}裏,遇到last後,本location{}裏後續rewrite/return規則不執行,但重寫後的url再次從頭開始執行全部規則,哪一個匹配執行哪一個。
相關文章
相關標籤/搜索