Rewite 規則作用
作用場景
URL訪問跳轉,支持開發設計,如頁面跳轉,兼容性支持,展示效果等
SEO優化
維護:後臺維護、流量轉發等
安全
注:nginx官方文檔:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
配置語法
Syntax: rewrite regex replacement [flag];
Default:——
Context:server、location、if
server {
rewrite {規則} {定向路徑} {重寫類型} ;
}
2、定向路徑:表示匹配到規則後要定向的路徑,如果規則裡有正則,則可以使用$index來表示正則裡的捕獲分組
last :相當於Apache裡德(L)標記,表示完成rewrite,瀏覽器地址欄URL地址不變。停止rewrite檢測【如果沒有匹配到,會繼續向下匹配】
break;本條規則匹配完成後,終止匹配,不再匹配後面的規則,瀏覽器地址欄URL地址不變。停止rewrite檢測【如果沒有匹配到,則不再向下匹配,直接返回結果404】
redirect:返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址。
permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址
簡單實例
rewrite ^(.*)$ /pages/maintain.html break;
解釋說明:
會把所有的請求都重定向到 /pages/maintain.html 頁面。
- 因為301和302不能簡單的只返回狀態碼,還必須有重定向的URL,這就是return指令無法返回301,302的原因了(return 只能返回除301、302之外的code)。
-
last一般寫在server和if中,而break一般使用在location中
3.last不終止重寫後的url匹配,即新的url會再從server走一遍匹配流程,而break終止重寫後的匹配
4.break和last都能組織繼續執行後面的rewrite指令 -
在location裡一旦返回break則直接生效並停止後續的匹配location
<pre><code>server { location / { rewrite /last/ /q.html last; rewrite /break/ /q.html break; } location = /q.html { return 400; }
}
訪問/last/時重寫到/q.html,然後使用新的uri再匹配,正好匹配到locatoin = /q.html然後返回了400;
訪問/break時重寫到/q.html,由於返回了break,則直接停止了;
常用的正則表達式
案例說明
server {
rewrite /last.html /index.html last;
# 訪問 /last.html 的時候,頁面內容重寫到 /index.html 中
rewrite /break.html /index.html break;
# 訪問 /break.html 的時候,頁面內容重寫到 /index.html 中,並停止後續的匹配
rewrite /redirect.html /index.html redirect;
# 訪問 /redirect.html 的時候,頁面直接302定向到 /index.html中
rewrite /permanent.html /index.html permanent;
# 訪問 /permanent.html 的時候,頁面直接301定向到 /index.html中
rewrite ^/html/(.+?).html$ /post/$1.html permanent;
# 把 /html/*.html => /post/*.html ,301定向
rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
# 把 /search/key => /search.html?keyword=key
}
server {
listen 80 default_server;
server_name www.zhangbiao.com;
access_log /var/log/nginx/log/host.access.log main;
root /opt/app/code;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 '{"status":"success"}';
}
}
訪問:http://www.zhangbiao.com/test/
訪問:[http://www.zhangbiao.com/last/](https://www.aliyun.com/minisite/goods?userCode=xjz7ghod)
訪問:[http://www.zhangbiao.com/break/](https://www.aliyun.com/minisite/goods?userCode=xjz7ghod)
可以發現訪問 last 的時候新建立了一個請求 /test/ ,而訪問/break/ 請求的時候 會去 /opt/app/code 下找相應的資源,沒找到所以報錯。
Rewrite規則_redirect和permanent區別
server {
listen 80 default_server;
server_name www.zhangbiao.com;
access_log /var/log/nginx/log/host.access.log main;
root /opt/app/code;
location ~ ^/imooc {
rewrite ^/imooc http://www.imooc.com/ permanent;
#rewrite ^/imooc http://www.imooc.com/ redirect;
}
}
redirect 表示臨時的重定向 ,只要後端服務是開者的。每次訪問 /imoc 都會重定向到 http://www.imooc.com
permanent 表示永久重定向,第一次訪問成功後,把後端服務關閉後,訪問/imoc 仍然會重定向到 http://www.imooc.com
server {
listen 80;
server_name www.zhangbiao.com;
root /opt/app/code;
location / {
rewrite ^/course-(\d+)-(\d+)-(\d+)\.html$ /course/$1/$2/course_$3.html break;
if ($http_user_agent ~* Chrome) {
rewrite ^/nginx http://coding.imooc.com/class/121.html redirect;
}
if (!-f $request_filename) {
rewrite ^/(.*)$ http://www.baidu.com/$1 redirect;
}
index index.html index.htm;
}
error_page 500 502 503 504 404 /50x.html;
}
訪問:http://www.zhangbiao.com/course-11-22-33.html
訪問在 /opt/app/code/course/11/22 下存在的資源文件
訪問:http://www.zhangbiao.com/course-11-22-5
訪問在 /opt/app/code/course/11/22 下不存在的資源文件
rewrite優先級規則
- 執行server 塊的rewrite 指令。將所有的網站都重定向同一個網站。
- 執行location匹配。
- 執行選定的location中的rewrite。