Nginx以其良好的并发性能,目前正在逐渐取代Apache成为大家的Web server首选,但是Nginx目前的中文资料很少,需要大家努力贡献。

下面我介绍一下Nginx的Rewrite模块设置及Wordpress和Discuz的示例。Nginx的Rewrite规则比Apache的简单灵活多了,从下面介绍可见一斑。

首先,Nginx可以用if进行条件匹配,语法规则类似C,举例如下:

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

其次,Nginx的Rewrite规则与Apache几乎完全一致,所不同的是最后的flag标记,举例如下:

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

flag标记有:

last 相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则
break 与last类似
redirect 返回302临时重定向
permanent 返回301永久重定向
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;
}

Discuz!的重定向规则:

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



原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags: ,
xuesheng
2009/02/27 12:43
假如我要屏蔽百度、yahoo的蜘蛛,
下面这样写对不对?
location ~* / {
if ($http_user_agent ~ baidu yahoo) {
return 503;
}
}
hqlulu 回复于 2009/02/27 16:54
看似就是这个意思,你可以测试一下
正则写法有点问题 ~ baidu|yahoo
xuesheng
2009/02/27 10:33

另外如果我要具体几个文件缓存的话,如何写
比如
location ~ .(a.txt|b.jpg|c.txt|d.bmp)$
{
expires      30d;
}

可以这样写吗?
xuesheng
2009/02/26 21:20
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
老大,这句放在哪里?
http、server?
具体该如何写
hqlulu 回复于 2009/02/27 09:44
你得看看nginx的语法
http{
  server{
    //放这里
  }
}

请参考Nginx官网资料:
http://wiki.codemongers.com/NginxModules
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐