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...
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/39884.html
序 本文主要解析一下ngx_http_rewrite_module中的rewrite相關配置。 directives 名稱 默認配置 作用域 官方說明 中文解讀 模塊 break 無 server, location, if Stops processing the current set of ngx_http_rewrite_module directives. 中斷當前的重寫 ng...
摘要:經由超文本傳輸協議通信,但是數據包由安全協議加密,實現加密數據與認證功能。該模塊指令定義相關設置證書文件,私鑰文件,會話緩存等內容。允許在客戶端建立會話時傳遞請求服務器名稱,這樣服務器就會知道該發送哪個虛擬主機下的證書文件。 1、nginx基本狀態信息頁面 配置示例: location /basic_status { stub_...
摘要:如果狀態碼附帶文字段落,該文本將被放置在響應主體。相反,如果狀態碼后面是一個,該將成為頭部值。沒有狀態碼的將被視為一個狀態碼,這種情況下需要以或者開頭。因為和不能簡單的只返回狀態碼,還必須有重定向的,這就是指令無法返回的原因了。 HTTP模塊(核心模塊,也是主要用到的模塊) server模塊 server模塊是http的子模塊,它用來定義一個虛擬主機 例子: server { ...
摘要:語法規則定向路徑重寫類型規則可以是字符串或者正則來表示想匹配的目標定向路徑表示匹配到規則后要定向的路徑,如果規則里有正則,則可以使用來表示正則里的捕獲分組重寫類型相當于里德標記,表示完成,瀏覽器地址欄地址不變本條規則匹配完成后,終止匹配,不 rewrite語法 server { rewrite {規則} {定向路徑} {重寫類型} ; } 1、規則:可以是字符串或者正則來表示想...
摘要:非標準碼關閉連接而不發送響應報頭。指令按照它們在配置文件中出現的順序執行??梢允褂脴酥緛斫K止指令的進一步處理。返回永久重定向。發送如下請求控制是否記錄有關未初始化變量的警告。 之前在配置nginx時,總是遇到rewrite指令的last和break標識的問題,看到的資料大都是last 基本上都用這個 Flag,break 中止 Rewirte,不在繼續匹配??赐曛筮€是有點懵,后來看了...
閱讀 1357·2021-11-24 09:39
閱讀 1346·2021-11-04 16:12
閱讀 2686·2021-09-24 09:47
閱讀 3337·2021-09-01 10:50
閱讀 1477·2019-08-30 15:55
閱讀 1423·2019-08-30 15:43
閱讀 642·2019-08-30 11:08
閱讀 3578·2019-08-23 18:33