配置示例
注:示例中使用了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]
說明:
- 將原始請(qǐng)求/noflag/a.html?key=value重寫為/test/a.html?capture=a.html&key=value
- 執(zhí)行了后續(xù)的ngx_http_rewrite_module模塊的指令set翩肌,將flag修改為rewrite noflag
- 沒有執(zhí)行后續(xù)的echo指令模暗,而是發(fā)起了內(nèi)部請(qǐng)求。新的請(qǐng)求匹配了location /test/念祭,該location返回了響應(yīng)兑宇。
- rewrite指令改變了變量$uri($document_uri)、$args($query_string)粱坤,但不會(huì)改變$request顾孽、$request_uri,這些變量可以輸出到access log中比规。
- 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]
說明:
- 將原始請(qǐng)求/last/a.html?key=value重寫為/test/a.html?capture=a.html&key=value
- 不再執(zhí)行后續(xù)的rewrite 模塊指令若厚,沒有修改flag的值。這是last標(biāo)志和不加標(biāo)志的區(qū)別蜒什。
- 沒有執(zhí)行后續(xù)的echo指令测秸,而是發(fā)起了內(nèi)部請(qǐng)求。新的請(qǐng)求匹配了location /test/灾常,該location返回了響應(yīng)霎冯。
- rewrite指令改變了變量$uri($document_uri)、$args($query_string)钞瀑,但不會(huì)改變$request沈撞、$request_uri,這些變量可以輸出到access log中雕什。
- 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]
說明:
- 將原始請(qǐng)求/break/a.html?key=value重寫為/test/a.html?capture=a.html&key=value
- 不再執(zhí)行后續(xù)的rewrite 模塊指令缠俺,沒有修改flag的值。
- 不發(fā)起新的請(qǐng)求贷岸。繼續(xù)執(zhí)行本location中的后續(xù)處理階段壹士,即返回了響應(yīng)。這是break標(biāo)志與last標(biāo)志的區(qū)別偿警。
- rewrite指令改變了變量$uri($document_uri)躏救、$args($query_string),但不會(huì)改變$request螟蒸、$request_uri盒使,這些變量可以輸出到access log中。
- nginx access log生成1條日志
●break + set指令
請(qǐng)求URL:http://example.com/break_set/a.html?key=value
結(jié)果:
flag=[break_set]
說明:
- 將原始請(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)凡泣。
- rewrite指令改變了變量$uri($document_uri)、$args($query_string)皮假,但不會(huì)改變$request鞋拟、$request_uri,這些變量可以輸出到access log中惹资。
- nginx access log生成1條日志
●break后無(wú)指令
請(qǐng)求URL:http://example.com/html/a.html?key=value
結(jié)果:
404 Not Found
說明:
- 將原始請(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澄成。
- rewrite指令改變了變量$uri($document_uri)、$args($query_string)畏吓,但不會(huì)改變$request墨状、$request_uri,這些變量可以輸出到access log中菲饼。
- 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]
說明:
- 直接返回客戶端302肾砂,并將重寫后的URL(http://example.com/test/a.html?capture=a.html&key=value)放到響應(yīng)頭的Location字段中。
- rewrite沒有修改$uri($document_uri)宏悦、$args($query_string)镐确、$request、$request_uri饼煞。access log中輸出的第一個(gè)請(qǐng)求的上述變量沒有修改辫塌。
- 如果是瀏覽器訪問,瀏覽器會(huì)使用rewrite后的URL重新發(fā)起請(qǐng)求派哲,并收到上述結(jié)果臼氨。
- 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]
說明:
- 直接返回客戶端301,并將重寫后的URL(http://example.com/test/a.html?capture=a.html&key=value)放到響應(yīng)頭的Location字段中褂乍。
- rewrite沒有修改$uri($document_uri)持隧、$args($query_string)、$request逃片、$request_uri屡拨。access log中輸出的第一個(gè)請(qǐng)求的上述變量沒有修改。
- 如果是瀏覽器訪問,瀏覽器會(huì)使用rewrite后的URL重新發(fā)起請(qǐng)求呀狼,并收到上述結(jié)果裂允。
- nginx access log生成2條日志,一次是原請(qǐng)求的哥艇,另一次是客戶端重新發(fā)起的請(qǐng)求绝编。