nginx配置指令rewrite的last、break、redirect、permanent參數(shù)詳解

配置示例

注:示例中使用了echo模塊,這樣可以直接看到響應(yīng)的內(nèi)容,以及變量修改情況

server {
    listen       80;
    server_name  example.com;
    set $flag    "org";

    location /noflag/ {
        set $flag "noflag";
        rewrite ^/noflag/(.*) /test/$1?capture=$1;
        set $flag "rewrite noflag";
        echo flag=[$flag];
        echo "noflag page";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }

    location /last/ {
        set $flag "last";
        rewrite ^/last/(.*) /test/$1?capture=$1 last;
        set $flag "rewrite last";
        echo flag=[$flag];
        echo "last page";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }

    location /break/ {
        set $flag "break";
        rewrite ^/break/(.*) /test/$1?capture=$1 break;
        set $flag "rewrite break";
        echo flag=[$flag];
        echo "break page";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }

    location /break_set/ {
        set $flag "break_set";
        rewrite ^/break_set/(.*) /test/$1?capture=$1 break;
        set $flag "rewrite break_set";
        echo flag=[$flag];
    }

    location /html/ {
        rewrite ^/html/(.*) /test/$1?capture=$1 break;
    }
    
    location /redirect/ {
        set $flag "redirect";
        rewrite ^/redirect/(.*) /test/$1?capture=$1 redirect;
        set $flag "rewrite redirect";
        echo flag=[$flag];
        echo "redirect page";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }

    location /permanent/ {
        set $flag "permanent";
        rewrite ^/permanent/(.*) /test/$1?capture=$1 permanent;
        set $flag "rewrite permanent";
        echo flag=[$flag];
        echo "permanent page";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }

    location /test/ {
        echo flag=[$flag];
        echo "test page";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }

    location / {
        echo flag=[$flag];
        echo "location /";
        echo request=[$request];
        echo request_uri=[$request_uri];
        echo uri=[$uri] args=[$args];
        echo document_uri=[$document_uri] query_string=[$query_string];
    }
}

請求演示

●無標(biāo)志
請求URL:http://example.com/noflag/a.html?key=value
結(jié)果:
flag=[rewrite noflag]
test page
request=[GET /noflag/a.html?key=value HTTP/1.1]
request_uri=[/noflag/a.html?key=value]
uri=[/test/a.html] args=[capture=a.html&key=value]
document_uri=[/test/a.html] query_string=[capture=a.html&key=value]
說明:

  1. 將原始請求/noflag/a.html?key=value重寫為/test/a.html?capture=a.html&key=value
  2. 執(zhí)行了后續(xù)的ngx_http_rewrite_module模塊的指令set,將flag修改為rewrite noflag
  3. 沒有執(zhí)行后續(xù)的echo指令,而是發(fā)起了內(nèi)部請求。新的請求匹配了location /test/,該location返回了響應(yīng)。
  4. rewrite指令改變了變量$uri($document_uri)、$args($query_string),但不會改變$request、$request_uri,這些變量可以輸出到access log中。
  5. nginx access log生成1條日志

●last
請求URL:http://example.com/last/a.html?key=value
結(jié)果:
flag=[last]
test page
request=[GET /last/a.html?key=value HTTP/1.1]
request_uri=[/last/a.html?key=value]
uri=[/test/a.html] args=[capture=a.html&key=value]
document_uri=[/test/a.html] query_string=[capture=a.html&key=value]
說明:

  1. 將原始請求/last/a.html?key=value重寫為/test/a.html?capture=a.html&key=value
  2. 不再執(zhí)行后續(xù)的rewrite 模塊指令,沒有修改flag的值。這是last標(biāo)志和不加標(biāo)志的區(qū)別。
  3. 沒有執(zhí)行后續(xù)的echo指令,而是發(fā)起了內(nèi)部請求。新的請求匹配了location /test/,該location返回了響應(yīng)。
  4. rewrite指令改變了變量$uri($document_uri)、$args($query_string),但不會改變$request、$request_uri,這些變量可以輸出到access log中。
  5. nginx access log生成1條日志

●break
請求URL:http://example.com/break/a.html?key=value
結(jié)果:
flag=[break]
break page
request=[GET /break/a.html?key=value HTTP/1.1]
request_uri=[/break/a.html?key=value]
uri=[/test/a.html] args=[capture=a.html&key=value]
document_uri=[/test/a.html] query_string=[capture=a.html&key=value]
說明:

  1. 將原始請求/break/a.html?key=value重寫為/test/a.html?capture=a.html&key=value
  2. 不再執(zhí)行后續(xù)的rewrite 模塊指令,沒有修改flag的值。
  3. 不發(fā)起新的請求。繼續(xù)執(zhí)行本location中的后續(xù)處理階段,即返回了響應(yīng)。這是break標(biāo)志與last標(biāo)志的區(qū)別。
  4. rewrite指令改變了變量$uri($document_uri)、$args($query_string),但不會改變$request、$request_uri,這些變量可以輸出到access log中。
  5. nginx access log生成1條日志

●break + set指令
請求URL:http://example.com/break_set/a.html?key=value
結(jié)果:
flag=[break_set]
說明:

  1. 將原始請求/break_set/a.html?key=value重寫為/test/a.html?capture=a.html&key=value,由于rewrite的flag參數(shù)是break,不發(fā)起新的請求也不再執(zhí)行后面的rewrite模塊的指令。繼續(xù)執(zhí)行本location中的后續(xù)處理階段,即返回了響應(yīng)。
  2. rewrite指令改變了變量$uri($document_uri)、$args($query_string),但不會改變$request、$request_uri,這些變量可以輸出到access log中。
  3. nginx access log生成1條日志

●break后無指令
請求URL:http://example.com/html/a.html?key=value
結(jié)果:
404 Not Found
說明:

  1. 將原始請求/html/a.html?key=value重寫為/test/a.html?capture=a.html&key=value,由于使用了break參數(shù),所以不會發(fā)起新的請求,由于沒有其他指令,所以會執(zhí)行該location下的默認(rèn)的content階段的指令,即嘗試找/test/a.html(這個路徑不是操作系統(tǒng)上的路徑,而是nginx靜態(tài)文件的路徑,默認(rèn)是以nginx安裝目錄下的html為根目錄)這個html頁面并輸出內(nèi)容,由于該頁面不存在,所以返回404。
  2. rewrite指令改變了變量$uri($document_uri)、$args($query_string),但不會改變$request、$request_uri,這些變量可以輸出到access log中。
  3. nginx access log生成1條日志

●redirect
請求URL:http://example.com/redirect/a.html?key=value
結(jié)果:
flag=[org]
test page
request=[GET /test/a.html?capture=a.html&key=value HTTP/1.1]
request_uri=[/test/a.html?capture=a.html&key=value]
uri=[/test/a.html] args=[capture=a.html&key=value]
document_uri=[/test/a.html] query_string=[capture=a.html&key=value]
說明:

  1. 直接返回客戶端302,并將重寫后的URL(http://example.com/test/a.html?capture=a.html&key=value)放到響應(yīng)頭的Location字段中。
  2. rewrite沒有修改$uri($document_uri)、$args($query_string)、$request、$request_uri。access log中輸出的第一個請求的上述變量沒有修改。
  3. 如果是瀏覽器訪問,瀏覽器會使用rewrite后的URL重新發(fā)起請求,并收到上述結(jié)果。
  4. nginx access log生成2條日志,一次是原請求的,另一次是客戶端重新發(fā)起的請求。

●permanent
請求URL:http://example.com/permanent/a.html?key=value
結(jié)果:
flag=[org]
test page
request=[GET /test/a.html?capture=a.html&key=value HTTP/1.1]
request_uri=[/test/a.html?capture=a.html&key=value]
uri=[/test/a.html] args=[capture=a.html&key=value]
document_uri=[/test/a.html] query_string=[capture=a.html&key=value]
說明:

  1. 直接返回客戶端301,并將重寫后的URL(http://example.com/test/a.html?capture=a.html&key=value)放到響應(yīng)頭的Location字段中。
  2. rewrite沒有修改$uri($document_uri)、$args($query_string)、$request、$request_uri。access log中輸出的第一個請求的上述變量沒有修改。
  3. 如果是瀏覽器訪問,瀏覽器會使用rewrite后的URL重新發(fā)起請求,并收到上述結(jié)果。
  4. nginx access log生成2條日志,一次是原請求的,另一次是客戶端重新發(fā)起的請求。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容