找回密码
 注册加入

扫一扫,极速登录

QQ登录

只需一步,快速开始

搜索
查看: 2881|回复: 1

几个Nginx子目录rewrite的例子

[复制链接]
发表于 2011-4-8 11:22:33 | 显示全部楼层 |阅读模式
我们已经有很多客户在 VPS 上使用 Nginx,对于刚从 Apache 转过来的客户最常遇到的一个问题就是怎么弄 Nginx 下的 rewrite 以及怎么把 Apache 里的 .htaccess 转化成 Nginx,网上关于这方面的资料一大堆,关于 wordpress, discuz, phpcms, ecshop, shopex 等的 rewrite 应有尽有,直接 copy 就可以。还有一个 Nginx 新手常见的问题是拿到这些 rewrite 规则后不知道怎么改,比如 Nginx 下子目录的 rewrite 应该改成什么样子?/ 下是 wordpress,/bbs 下装个 discuz,/ 是 discuz,/blog 下装个 wordpress 或者 / 下是 wordpress,/blog 下再装个 wordpress 等,这样的 rewrite 怎么改呢?弄几个例子放到我们的 FAQ 里供参考:

WordPress 安装在子目录 /blog 下:
  1. location /blog/ {
  2. root /home/www/vpsee.com;
  3. index index.php index.html index.htm;
  4. if (!-e $request_filename) {
  5. rewrite ^.+/?(/blog/wp-.*) $1 last;
  6. rewrite ^.+/?(/blog/.*\.php)$ $1 last;
  7. rewrite ^(.+)$ /blog/index.php?q=$1 last;
  8. }
  9. }
复制代码
Discuz! 7.2 安装在子目录 /bbs 下:
  1. location /bbs/ {
  2. root /home/www/vpsee.com;
  3. index index.php index.html index.htm;
  4. rewrite ^/bbs/archiver/((fid|tid)-[\w\-]+\.html)$ /bbs/archiver/index.php?$1 last;
  5. rewrite ^/bbs/forum-([0-9]+)-([0-9]+)\.html$ /bbs/forumdisplay.php?fid=$1&page=$2 last;
  6. rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /bbs/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
  7. rewrite ^/bbs/space-(username|uid)-(.+)\.html$ /bbs/space.php?$1=$2 last;
  8. rewrite ^/bbs/tag-(.+)\.html$ /bbs/tag.php?name=$1 last;
  9. }
复制代码
Discuz! X1.5 安装在子目录 /bbs 下:
  1. location /bbs/ {
  2. root /home/www/vpsee.com;
  3. index index.php index.html index.htm;
  4. rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
  5. rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
  6. rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
  7. rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
  8. rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
  9. rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
  10. rewrite ^([^\.]*)/([a-z]+)-(.+)\.html$ $1/$2.php?rewrite=$3 last;
  11. if (!-e $request_filename) {
  12. return 404;
  13. }
  14. }
复制代码
如果对理解 ^([^\.]*)/([a-z]+)-(.+)\.html$ 这样的正则表达式有困难并对这方面有兴趣的话可以看看一些书,最好的一本应该是 O’Reilly 出的 Mastering Regular Expressions(也有中文版:《精通正则表达式》)。

发表于 2011-4-8 20:37:06 | 显示全部楼层
hello,有没有好的国内的vps推荐个


                               
登录/注册后可看大图
来自 PPfield 的新浪微博
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册加入  

本版积分规则

Archiver|手机版|小黑屋|Discuz!扩展中心 ( 浙ICP备14042422号-1 )|网站地图QQ机器人

GMT+8, 2024-5-15 14:41 , Processed in 0.169585 second(s), 13 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表