Nginx 僞靜態Rewrite,重定向Location配置總結(轉)

語法規則: location [=|~|~*|^~] /uri/ { … } php

= 開頭表示精確匹配
^~ 開頭表示uri以某個常規字符串開頭,理解爲匹配 url路徑便可。nginx不對url作編碼,所以請求爲/static/20%/aa,能夠被規則^~ /static/ /aa匹配到(注意是空格)。
~ 開頭表示區分大小寫的正則匹配
~* 開頭表示不區分大小寫的正則匹配
!~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配 的正則
/ 通用匹配,任何請求都會匹配到。
多個location配置的狀況下匹配順序爲(參考資料而來,還未實際驗證,試試就知道了,沒必要拘泥,僅供參考):
首先匹配 =,其次匹配^~, 其次是按文件中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,中止匹配,按當前匹配規則處理請求。
例子,有以下匹配規則:
location = / {
 #規則A
}
location = /login {
 #規則B
}
location ^~ /static/ {
 #規則C
}
location ~ \.(gif|jpg|png|js|css)$ {
 #規則D
}
location ~* \.png$ {
 #規則E
}
location !~ \.xhtml$ {
 #規則F
}
location !~* \.xhtml$ {
 #規則G
}
location / {
 #規則H
}
那麼產生的效果以下:
訪問根目錄/, 好比 http://localhost/ 將匹配規則A
訪問  http://localhost/login 將匹配規則B, http://localhost/register 則匹配規則H
訪問  http://localhost/static/a.html 將匹配規則C
訪問  http://localhost/a.gifhttp://localhost/b.jpg 將匹配規則D和規則E,可是規則D順序優先,規則E不起做用,而  http://localhost/static/c.png 則優先匹配到規則C
訪問  http://localhost/a.PNG 則匹配規則E,而不會匹配規則D,由於規則E不區分大小寫。
訪問  http://localhost/category/id/1111 則最終匹配到規則H,由於以上規則都不匹配,這個時候應該是nginx轉發請求給後端應用服務器,好比FastCGI(php),tomcat(jsp),nginx做爲方向代理服務器存在。
因此實際使用中,我的以爲至少有三個匹配規則定義,以下:
#直接匹配網站根,經過域名訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說。
#這裏是直接轉發給後端應用服務器了,也能夠是一個靜態首頁
# 第一個必選規則
location = / {
 proxy_pass  http://tomcat:8080/index
}
# 第二個必選規則是處理靜態文件請求,這是nginx做爲http服務器的強項
# 有兩種配置模式,目錄匹配或後綴匹配,任選其一或搭配使用
location ^~ /static/ {
 root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
 root /webroot/res/;
}
#第三個規則就是通用規則,用來轉發動態請求到後端應用服務器
#非靜態文件請求就默認是動態請求,本身根據實際把握
#畢竟目前的一些框架的流行,帶.php,.jsp後綴的狀況不多了
location / {
 proxy_pass  http://tomcat:8080/
}
未試驗過的其餘信息:
3、ReWrite語法
last – 基本上都用這個Flag。
break – 停止Rewirte,不在繼續匹配
redirect – 返回臨時重定向的HTTP狀態302
permanent – 返回永久重定向的HTTP狀態301
一、下面是能夠用來判斷的表達式:
-f和!-f用來判斷是否存在文件
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在文件或目錄
-x和!-x用來判斷文件是否可執行
二、下面是能夠用做判斷的全局變量
$host:localhost
$server_port:88
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
4、Redirect語法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ 「^star\.igrow\.cn$&quot {
rewrite ^(.*)  http://star.igrow.cn$1 redirect;
}
}
5、防盜鏈location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/  http://$host/logo.png;
}
}
6、根據文件類型設置過時時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
7、禁止訪問某個目錄
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
++ 一些可用的全局變量
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri
====================================

Nginx以其良好的併發性能,目前 正在逐漸取代Apache成爲你們的Web server首 選,可是Nginx目前的中文資料不多,須要你們努力貢獻。51Testing軟件測試網 t Q C^?w?g4G css

下面我介紹一下Nginx的Rewrite模 塊設置及Wordpress和Discuz的示例。Nginx的Rewrite規則比Apache的簡單靈活多了,從下面介紹可見一斑。 html

;P%r z T k Y \0首先,Nginx能夠用if進行條件匹配,語法規則相似C,舉例以下:51Testing 軟件測試網 X7s0P/y ~ G linux

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

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

G(u’@1\-p3}d M0 web

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

二、文件及目錄匹配,其中:51Testing軟件測試網 g,I X ](V O {:|*v p4V 正則表達式

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

如: 後端

‘OF7X l E f,[4{0 tomcat

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

其次,Nginx 的Rewrite規則與Apache幾乎徹底一致,所不一樣的是最後的flag標記,舉例以下:51Testing 軟件測試網1x n ~ t:^J$d P z 服務器

rewrite ^/feed/$ http://feed.shunz.net last;51Testing軟件測試網0} o ` Ib s4A8[;N

flag標記有:

f D"d.?-W X B4@0

  • last 至關於Apache裏的[L]標記,表示完成rewrite,再也不匹配後面的規則
  • break 與last相似
  • redirect 返回302臨時重定向
  • permanent 返回301永久重定向

記住下面幾個Nginx的Flags:
last – 基本上都用這個Flag。
break – 停止Rewirte,不在繼續匹配
redirect – 返回臨時重定向的HTTP狀態302
permanent – 返回永久重定向的HTTP狀態30151Testing軟件測試網 { | ^+D V

另 外,有個東西很關鍵,曾經摺騰我好幾個小時才搞定,就是Nginx裏面配置 {m,n} 這樣的正則規則的時候,條件必須加上雙引號,不然老是報錯沒法經過,官方文檔裏面真是很難找到這些東西,很暈。

WordPress的重定向規 則:

Y ^*T [&s+?3[ z m4b8p+L0

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; }

Discuz!的 重定向規則:51Testing軟件測試網5S2[ f9C'K e%i'\

if(!-f $request_filename){ rewrite^/archiver/((fid|tid)-[\w\-]+\.html)$/archiver/index.php?$1 last; rewrite^/forum-([0-9]+)-([0-9]+)\.html$/forumdisplay.php?fid=$1&page=$2 last; rewrite^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last; rewrite^/space-(username|uid)-(.+)\.html$/space.php?$1=$2 last; rewrite^/tag-(.+)\.html$/tag.php?name=$1 last; } 

首先Apache的 Rewite規則差異不是很大,可是Nginx的Rewrite規則比Apache的簡單靈活多了
(K ^'H#l B Q'L7I9Y(o0Nginx能夠用if進行條件匹配,語法規則相似C

g k c }(x O$t+? t051Testing軟件測試網 I4q1Z P I3G p |
if ($http_user_agent ~ MSIE) {
0@*B B0v m4Y1A8p0rewrite ^(.*)$ /msie/$1 break;
? _ B g.m H } O e0}51Testing軟件測試網1z?_8M E*M,q |

HE#B"U ] F;_ X0官方文檔請點擊這裏51Testing軟件測試網*u'L*p9~ ] m:L
Rewrite的Flags51Testing軟件測試網 F,}+C g eR/Yn e

Flags can be any of the following:
p k5P8p { Z z'H0* last - completes processing of rewrite directives, after which searches for corresponding URI and location51Testing軟件測試網 u$E v0y o z*X
* break - completes processing of rewrite directives51Testing 軟件測試網 QW ] j,f:a E U _ V1z
*redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://51Testing軟件測試網 [+Q f ~+X O @R
* permanent - returns permanent redirect with code 30151Testing軟件測試網 u T f d/v1Z`+@

last - 完成重寫指令後,搜索相應的URI和位置。至關於Apache裏的[L]標記,表示完成rewrite,再也不匹配後面的規則。
3E!_4G1Z9b E Y \ P `:r0break - 停止Rewirte,不在繼續匹配。
t&X j S2N5t c z0redirect - 返回臨時重定向的HTTP狀態302。
7t&r q B)Z0permanent - 返回永久重定向的HTTP狀態301。

~ i5J C u O0ZEND Framework的重定向規則:51Testing 軟件測試網 | f {._/G O
案例一:51Testing軟件 測試網 c3s f | l V-a5Q
所有重定向到 /index.php51Testing 軟件測試網"N n ~ F7L-o c8n2R&q
rewrite ^/(.*) /index.php?$1&;51Testing軟件測試網 0^,d3Z+d C'T4w H
案例二:51Testing軟件測 試網 m#I {%TX
若是文件或目錄不存在則重定向到index.php51Testing軟件測試網:X1g _ Q `1h [ |;G
if (!-e $request_filename) {51Testing軟件測試網5B1x K `5t!p$O
rewrite ^/(.*) /index.php?$1&;51Testing軟件測試網 j5}.KH ~ j3l K(^)P e
}

7Y!Y U R"K X"W0Wordpress的重定向規則:
+y o$z s {0案例一:51Testing軟件測試網 3l$a z v k s [ N-U K
http://www.wemvc.com/12 重定向到 http://www.wemvc.com/index.php?p=1251Testing 軟件測試網5S ^ sk k \b s
if (!-e $request_filename) {51Testing軟件測試網3V L7w h y0v ~!J V:V a
rewrite ^/(.+)$ /index.php?p=$1 last;
X;_"w L+@(q0}
3e @ |:C2M0案例二:51Testing軟件測試網 U d } L5D
與zendframework配置很像
K P E ["J0if (!-e $request_filename) {51Testing軟件測試網 J1U'Z [/Z+K0S H9Z ^
rewrite ^/(.*) /index.php?$1&;51Testing軟件測試網 cG8_1Z y [ ?;x0p e
}51Testing軟件測試網 J rB R f6h1\'b

如下爲Discuz完整的Rewrite for Nginx規則51Testing軟件測試網0B E @-W(T `:e`'N5K)H l T y*^
if (!-f $request_filename) {51Testing軟件測試網} U2W9k D;m"t
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;51Testing軟件測試網 8@ Z d C'D%q!w%M
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
4P i4h F t?Z d0rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page=$3&page=$2 last;
}5a1f Z U$@ g0rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;51Testing軟件測試網 l l \ S%n6G P&P
}51Testing軟件測試網 @ `&I W d*e | w(E K

文件及目錄匹配,其 中:51Testing軟件測試網 J+J a @ i*v p
-f和!-f用來判斷是否存在文件
*t y S ~ O O$F0-d和!-d用來判斷是否存在目錄51Testing軟件測試網 8oL m0O `'x9I6D
-e和!-e用來判斷是否存在文件或目錄
|2K9{-[ E2k0-x和!-x用來判斷文件是否可執行

2E6j$C%V M'K#v H {0正則表達式所有符號解 釋 @2] @.Y/i0~ 爲區分大小寫匹配 ~ P R J X0~* 爲不區分大小寫匹配51Testing軟件測試網,| t7w r&D I !~和!~* 分別爲區分大小寫不匹配及不區分大小寫不匹配51Testing軟件測試網3b p ` X$N-v rN*J (pattern) 匹配 pattern 並獲取這一匹配。所獲取的匹配能夠從產生的 Matches 集合獲得,在VBScript. 中使用 SubMatches 集合,在JScript. 中則使用 $0…$9 屬性。要匹配圓括號字符,請使用 ‘\(’ 或 ‘\)’。51Testing軟件測試網:z j$T u X Y E M G1S ^ 匹配輸入字符串的開始位置。51Testing 軟件測試網:V z!w$S;^%B $ 匹配輸入字符串的結束位置。

相關文章
相關標籤/搜索