Nginx的rewrite應用

Rewrite主要的功能是實現URL重寫,Nginx 的 Rewrite 規則採用 PCRE Perl 兼容正則表達式的語法進行規則匹配,如相使用 Nginx 的 Rewrite 功能,在編譯 Nginx 前要編譯安裝 PCRE 庫。


一,Nginx使用if進行條件匹配

Nginx可以用if進行條件匹配,語法規則類似C

if (條件){...} ( 可用於: server,location )  ## 檢查一個條件是否符合,如果條件符合,則執行大括號內的語句。不支持嵌套,不支持多條件 && || 

例如:if ($http_user_agent ~ MSIE) {rewrite  ^(.*)$  /msie/$1  break;}

1、正則表達式匹配,其中:

~  爲區分大小寫匹配 
~* 爲不區分大小寫匹配 
!~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配

2、文件及目錄匹配,其中:

-f和!-f用來判斷是否存在文件 
-d和!-d用來判斷是否存在目錄 
-e和!-e用來判斷是否存在文件或目錄 
-x和!-x用來判斷文件是否可執行

例如:if (!-f $request_filename) {proxy_pass  http://127.0.0.1;}

Wordpress的重定向規則:

if (!-e $request_filename) {rewrite ^/(index|atom|rsd)\.xml$ http://feed.shunz.net last;rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;rewrite ^ /index.php last;}


return ( 可用於: server,location,if )  ## 用於結束規則的執行並反回狀態碼給客戶端。  狀態碼可以是 :204/400/402~406/408/410/411/413/416/500~504  

如:location ~ .*\.(sh|bash)?$ {  
      return 403;  
    }  
## 訪問的 URL 以 .sh .bash 結尾的,則返回 403 。

204  No Content  
400  Bad Request  
402  Payment Required  
403  Forbidden  
404  Not Found  
405  Method Not Allowed  
406  Not Acceptable  
408  Request Timeout  
410  Gone  
411  Length Required  
413  Request Entity Too Large  
416  Requested Range Not Satisfiable  
500  Internal Server Error  
501  Not Implemented  
502  Bad Gateway  
503  Service Unavailable  
504  Gateway Timeout 


二,Nginx使用rewrite

rewrite ( 可用於: server,location,if ) ## 重寫 URL ,或修改字符串。重寫 URL 只對相對路徑有效,如想對主機名,要使用 if 語句。 


例1:
if ($host ~* www\.(.*)) {  
      set $host_without_www $1;  
      rewrite ^(.*)$ http://$host_without_www$1 permanent;  
}  
## 如果替換串以 http:// 開頭,將會採用 301 或 302 跳轉進行 URL 重定向。 

例2:
rewrite ^/feed/$ http://feed.shunz.net last;


Nginx的Rewrite規則與Apache幾乎完全一致,所不同的是最後的flag標記,Nginx的rewrite指令後支持的標記有last,break,redirect,permanent

last 相當於Apache裏的[L]標記,表示完成rewrite,不再匹配後面的規則 
break ( 可用於: server,location,if ) ## 本條規則匹配完成後,終止匹配,不再匹配後面的規則

如:rewrite ^/b/(.*)\.html /play.php?video=$1 break; 

redirect 返回302臨時重定向,瀏覽器會顯示跳轉後的URL地址
permanent 返回301永久重定向,瀏覽器會顯示跳轉後的URL地址

last/break用來實現URL重寫,瀏覽器地址欄的URL不變,但在服務器端訪問的路徑發生了變化。
redirect/permanent實現URL跳轉,瀏覽器地址欄URL會顯示跳轉後的URL。
使用 alias 指令時必須用 last 標記 ,使用 proxy_pass 指令時要用 break 表示。last 標記在本條 rewrite 規則執行完畢後,會對其所在 server{....}標籤重新發起請求,而 break 標記則在本條規則匹配完成後,終止匹配。  

如:location /cms/ {  
      proxy_pass http://test.abc.com;  
      rewrite "^/cms/(.*)\.html$" /cms/index.html break;  
}  

## 這條規則如果使用 last 會導致死循環。  
## 一般在根 location 中(即 location / {....})或直接在 server 標籤編寫 rewrite 規則,推薦使用 last 標記,  
       在非根 location 中 (即 location /cms/ {...}),則使用 break 標記。 


Nginx $document_uri參數的使用

$document_uri  表示訪問的url 現在我的需求是,訪問 www.abc.com  請求到 www.abc.com/abc/

在nginx配置文件中加入


   if ($document_uri !~ 'abc')
    {
            rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;
    }


而不是單獨加一句  rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;
如果只加rewrite 規則,而不限定條件,那麼會造成死循環。  會訪問到   http://www.abc.com/abc/abc/abc/abc/....


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章