十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

Nginx重定向[Rewrite]配置

上一篇 / 下一篇  2011-06-19 12:23:25 / 个人分类:nginx

首先Apache的Rewite规则差别不是很大,但是Nginx的Rewrite规则比Apache的简单灵活多了51Testing软件测试网$T.i.menc~ZS
Nginx可以用if进行条件匹配,语法规则类似C51Testing软件测试网^Zg~$E'MI `Z)t

51Testing软件测试网{m H+YIJ8G
if ($http_user_agent ~ MSIE) {
\2yBb#oa0rewrite ^(.*)$ /msie/$1 break;
R9@3cFk(j6[rz0}51Testing软件测试网ll3y7pe Sq

(Og`N#[P0官方文档请点击这里51Testing软件测试网e,b,oOy Q @`
Rewrite的Flags

m b MF$b4Q0

Flags can be any of the following:
RY1Pl"EK0* last - completes processing of rewrite directives, after which searches for corresponding URI and location51Testing软件测试网:{[?.w `,@
* break - completes processing of rewrite directives51Testing软件测试网 ]4R/kC}(n7dXLy
*redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://51Testing软件测试网,X!L!Jk O1AY
* permanent - returns permanent redirect with code 301
51Testing软件测试网8@bW?vGt

last - 完成重写指令后,搜索相应的URI和位置。相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则。
6yXuYl3{0break - 中止Rewirte,不在继续匹配。
5~j)?AO4l0redirect - 返回临时重定向的HTTP状态302。
#gQ~V2Q z(Z3`b0permanent - 返回永久重定向的HTTP状态301。51Testing软件测试网g$q ~ d-LD

ZEND Framework的重定向规则:
Zu-T j ay$h0`0案例一:51Testing软件测试网.D3\U,H!a!\
全部重定向到 /index.php
YA J]%h^?O6R0rewrite ^/(.*) /index.php?$1&;51Testing软件测试网lT7`X5wX*M-L9G
案例二:
le6Vt#|cz0如果文件或目录不存在则重定向到index.php
!]/|9{:W)Es$Vx0if (!-e $request_filename) {51Testing软件测试网j0mJcAf~
rewrite ^/(.*) /index.php?$1&;51Testing软件测试网 c*`H;ys7EM:J
}

(\2_,B!Y6g*p(w1E,Z0

Wordpress的重定向规则:
n&Q F9\v/Im1L ^0i t0案例一:
F!WQ%I o d0@C0http://www.wemvc.com/12 重定向到 http://www.wemvc.com/index.php?p=1251Testing软件测试网` @'}4l'Jw6_0Z|:|Be
if (!-e $request_filename) {
XNjp5Q:c*h.j0rewrite ^/(.+)$ /index.php?p=$1 last;51Testing软件测试网Q{ } Dm4Y Wu%A
}

cO X6fhD0案例二:51Testing软件测试网5e2B)L x[$ZW
与zendframework配置很像51Testing软件测试网"B:u y)o)D
if (!-e $request_filename) {
-H8IW&Qa)Z U8_R0rewrite ^/(.*) /index.php?$1&;51Testing软件测试网i(Si6yq ]/j$FH
}

A$ws!N.s\(J0

以下为Discuz完整的Rewrite for Nginx规则51Testing软件测试网 H0~sB*^}"`;z
if (!-f $request_filename) {
$jrr5R(p8Ck,?0rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
+uu7~-~ DU2_0rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
0Qi,am-cc0rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page=$3&page=$2 last;51Testing软件测试网0Y!V8ti~ E-i
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
*bO ]V8t5o0}

+io'u"~.BD(A0

文件及目录匹配,其中:51Testing软件测试网Z+t!R"ZAsRFp
-f和!-f用来判断是否存在文件51Testing软件测试网.w&{XrZj
-d和!-d用来判断是否存在目录51Testing软件测试网A'}I4B.zQ0_
-e和!-e用来判断是否存在文件或目录51Testing软件测试网CBG6N#T)nb
-x和!-x用来判断文件是否可执行51Testing软件测试网.t9h2?E @;G'Q

正则表达式全部符号解释
3VkxUUDm'xK0~ 为区分大小写匹配
bx-m#P Qz4Rn0~* 为不区分大小写匹配
eIYC JKN(nc$^1e0!~和!~* 分别为区分大小写不匹配及不区分大小写不匹配51Testing软件测试网fe vKL6HS!N
(pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript. 中使用 SubMatches 集合,在JScript. 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘\(’ 或 ‘\)’。51Testing软件测试网7b-V(k,I8MH?
^ 匹配输入字符串的开始位置。51Testing软件测试网aQT f\ nq
$ 匹配输入字符串的结束位置。

BMN$F(~,w0

TAG: ginx

 

评分:0

我来说两句

Open Toolbar