Example 1: No (break or last) flags
server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; } location /notes { echo "finally matched location /notes"; } location /documents { echo "finally matched location /documents"; } rewrite ^/([^/]+.txt)$ /notes/$1; rewrite ^/notes/([^/]+.txt)$ /documents/$1; }Result:
#curl example.com/test.txt finally matched location /documentsExplanation:
For rewrite, the flags are optional!
Example 2: Outside location block (break or last)server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; } location /notes { echo "finally matched location /notes"; } location /documents { echo "finally matched location /documents"; } rewrite ^/([^/]+.txt)$ /notes/$1 break; # or last rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed }Result:
#curl example.com/test.txt finally matched location /notesExplanation:
Outside the location block, both break and last behave in the exact manner...
no more parsing of rewrite conditions
Nginx internal engine goes to the next phase (searching for location match)
server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; rewrite ^/([^/]+.txt)$ /notes/$1 break; rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed } location /notes { echo "finally matched location /notes"; } location /documents { echo "finally matched location /documents"; } }Result:
# curl example.com/test.txt finally matched location /Explanation:
Inside a location block, break flag would do the following...
no more parsing of rewrite conditions
Nginx internal engine continues to parse the current location block
server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; rewrite ^/([^/]+.txt)$ /notes/$1 last; rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed } location /notes { echo "finally matched location /notes"; rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed, either! } location /documents { echo "finally matched location /documents"; } }Result:
# curl example.com/test.txt finally matched location /notesExplanation:
Inside a location block, last flag would do the following...
no more parsing of rewrite conditions
Nginx internal engine starts to look for another location match based on the result of the rewrite result.
no more parsing of rewrite conditions, even on the next location match!
When a rewrite condition with the flag break or last matches, Nginx stops parsing any more rewrites!
Outside a location block, with break or last, Nginx does the same job (stops processing anymore rewrite conditions).
Inside a location block, with break, Nginx only stops processing anymore rewrite conditions
Inside a location block, with last, Nginx stops processing anymore rewrite conditions and then starts to look for a new matching of location block! Nginx also ignores any rewrites in the new location block!
missed to include some more edge cases (actually common problem with rewrites, such as 500 internal error). But, that"d be out of scope of this question. Probably, example 1 is out of scope, too!
鏈接:https://serverfault.com/quest...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/39884.html
序 本文主要解析一下ngx_http_rewrite_module中的rewrite相關(guān)配置。 directives 名稱 默認(rèn)配置 作用域 官方說明 中文解讀 模塊 break 無 server, location, if Stops processing the current set of ngx_http_rewrite_module directives. 中斷當(dāng)前的重寫 ng...
摘要:經(jīng)由超文本傳輸協(xié)議通信,但是數(shù)據(jù)包由安全協(xié)議加密,實(shí)現(xiàn)加密數(shù)據(jù)與認(rèn)證功能。該模塊指令定義相關(guān)設(shè)置證書文件,私鑰文件,會(huì)話緩存等內(nèi)容。允許在客戶端建立會(huì)話時(shí)傳遞請(qǐng)求服務(wù)器名稱,這樣服務(wù)器就會(huì)知道該發(fā)送哪個(gè)虛擬主機(jī)下的證書文件。 1、nginx基本狀態(tài)信息頁面 配置示例: location /basic_status { stub_...
摘要:如果狀態(tài)碼附帶文字段落,該文本將被放置在響應(yīng)主體。相反,如果狀態(tài)碼后面是一個(gè),該將成為頭部值。沒有狀態(tài)碼的將被視為一個(gè)狀態(tài)碼,這種情況下需要以或者開頭。因?yàn)楹筒荒芎唵蔚闹环祷貭顟B(tài)碼,還必須有重定向的,這就是指令無法返回的原因了。 HTTP模塊(核心模塊,也是主要用到的模塊) server模塊 server模塊是http的子模塊,它用來定義一個(gè)虛擬主機(jī) 例子: server { ...
摘要:語法規(guī)則定向路徑重寫類型規(guī)則可以是字符串或者正則來表示想匹配的目標(biāo)定向路徑表示匹配到規(guī)則后要定向的路徑,如果規(guī)則里有正則,則可以使用來表示正則里的捕獲分組重寫類型相當(dāng)于里德標(biāo)記,表示完成,瀏覽器地址欄地址不變本條規(guī)則匹配完成后,終止匹配,不 rewrite語法 server { rewrite {規(guī)則} {定向路徑} {重寫類型} ; } 1、規(guī)則:可以是字符串或者正則來表示想...
摘要:非標(biāo)準(zhǔn)碼關(guān)閉連接而不發(fā)送響應(yīng)報(bào)頭。指令按照它們?cè)谂渲梦募谐霈F(xiàn)的順序執(zhí)行??梢允褂脴?biāo)志來終止指令的進(jìn)一步處理。返回永久重定向。發(fā)送如下請(qǐng)求控制是否記錄有關(guān)未初始化變量的警告。 之前在配置nginx時(shí),總是遇到rewrite指令的last和break標(biāo)識(shí)的問題,看到的資料大都是last 基本上都用這個(gè) Flag,break 中止 Rewirte,不在繼續(xù)匹配??赐曛筮€是有點(diǎn)懵,后來看了...
閱讀 1368·2021-11-24 09:39
閱讀 1358·2021-11-04 16:12
閱讀 2701·2021-09-24 09:47
閱讀 3346·2021-09-01 10:50
閱讀 1487·2019-08-30 15:55
閱讀 1432·2019-08-30 15:43
閱讀 652·2019-08-30 11:08
閱讀 3588·2019-08-23 18:33