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];
    }
}

請(qǐng)求演示

●無(wú)標(biāo)志
請(qǐng)求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. 將原始請(qǐng)求/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)部請(qǐng)求。新的請(qǐng)求匹配了location /test/念祭,該location返回了響應(yīng)兑宇。
  4. rewrite指令改變了變量$uri($document_uri)、$args($query_string)粱坤,但不會(huì)改變$request顾孽、$request_uri,這些變量可以輸出到access log中比规。
  5. nginx access log生成1條日志

●last
請(qǐng)求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. 將原始請(qǐng)求/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)部請(qǐng)求。新的請(qǐng)求匹配了location /test/灾常,該location返回了響應(yīng)霎冯。
  4. rewrite指令改變了變量$uri($document_uri)、$args($query_string)钞瀑,但不會(huì)改變$request沈撞、$request_uri,這些變量可以輸出到access log中雕什。
  5. nginx access log生成1條日志

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

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

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

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

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

●redirect
請(qǐng)求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中輸出的第一個(gè)請(qǐng)求的上述變量沒有修改辫塌。
  3. 如果是瀏覽器訪問,瀏覽器會(huì)使用rewrite后的URL重新發(fā)起請(qǐng)求派哲,并收到上述結(jié)果臼氨。
  4. nginx access log生成2條日志,一次是原請(qǐng)求的芭届,另一次是客戶端重新發(fā)起的請(qǐng)求储矩。

●permanent
請(qǐng)求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中輸出的第一個(gè)請(qǐng)求的上述變量沒有修改。
  3. 如果是瀏覽器訪問,瀏覽器會(huì)使用rewrite后的URL重新發(fā)起請(qǐng)求呀狼,并收到上述結(jié)果裂允。
  4. nginx access log生成2條日志,一次是原請(qǐng)求的哥艇,另一次是客戶端重新發(fā)起的請(qǐng)求绝编。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市貌踏,隨后出現(xiàn)的幾起案子十饥,更是在濱河造成了極大的恐慌,老刑警劉巖祖乳,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逗堵,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡眷昆,警方通過查閱死者的電腦和手機(jī)砸捏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)隙赁,“玉大人垦藏,你說我怎么就攤上這事∩》茫” “怎么了掂骏?”我有些...
    開封第一講書人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)厚掷。 經(jīng)常有香客問我弟灼,道長(zhǎng),這世上最難降的妖魔是什么冒黑? 我笑而不...
    開封第一講書人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任田绑,我火速辦了婚禮,結(jié)果婚禮上抡爹,老公的妹妹穿的比我還像新娘掩驱。我一直安慰自己,他們只是感情好冬竟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開白布欧穴。 她就那樣靜靜地躺著,像睡著了一般泵殴。 火紅的嫁衣襯著肌膚如雪涮帘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,554評(píng)論 1 305
  • 那天笑诅,我揣著相機(jī)與錄音调缨,去河邊找鬼疮鲫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛弦叶,可吹牛的內(nèi)容都是我干的俊犯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼湾蔓,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了砌梆?” 一聲冷哼從身側(cè)響起默责,我...
    開封第一講書人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咸包,沒想到半個(gè)月后桃序,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡烂瘫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年媒熊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坟比。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡芦鳍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出葛账,到底是詐尸還是另有隱情柠衅,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布籍琳,位于F島的核電站菲宴,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏趋急。R本人自食惡果不足惜喝峦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望呜达。 院中可真熱鬧谣蠢,春花似錦、人聲如沸查近。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)嗦嗡。三九已至勋锤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間侥祭,已是汗流浹背叁执。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工茄厘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谈宛。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓次哈,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親吆录。 傳聞我的和親對(duì)象是個(gè)殘疾皇子窑滞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容