nginx rewrite模塊
阿新 • • 發佈:2019-05-08
狀態 並且 regex 永久 版本 gin last reg 模塊
return
從0.8.42版本開始, return 語句可以指定重定向 url (狀態碼可以為如下幾種 301,302,303,307),
也可以為其他狀態碼指定響應的文本內容,並且重定向的url和響應的文本可以包含變量。
# return code [text]; 返回 ok 給客戶端 location = /ok { return 200 "ok"; } # return code URL; 臨時重定向到 百度 location = /redirect { return 302 http://www.baidu.com; } # return URL; 和上面一樣 默認也是臨時重定向 location = /redirect { return http://www.baidu.com; }
rewrite
rewrite regex replacement [flag];
regex 來匹配請求的url,如果匹配成功,則使用replacement更改URI。可以使用flag標誌來終止指令的進一步處理
- last,停止處理當前的ngx_http_rewrite_module的指令集,並開始搜索與更改後的URI相匹配的location;
- break,停止處理當前的ngx_http_rewrite_module指令集,就像上面說的break指令一樣;
- redirect,返回302臨時重定向。
- permanent,返回301永久重定向。
小栗子
location = / { rewrite / /www/index.html redirect; }
nginx rewrite模塊