娱乐新地带论坛

娱乐新地带论坛 (https://bbs.1819.net/index.php)
-   『软件使用』 (https://bbs.1819.net/forumdisplay.php?f=11)
-   -   Linux 几个 Nginx 子目录 伪静态(rewrite) 的例子 (https://bbs.1819.net/showthread.php?t=74042)

Tony 2011-06-24 01:16 PM

几个 Nginx 子目录 伪静态(rewrite) 的例子
 
现在已经有很多客户在 VPS 上使用 Nginx,对于刚从 Apache 转过来的客户最常遇到的一个问题就是怎么弄 Nginx 下的 rewrite 以及怎么把 Apache 里的 .htaccess 转化成 Nginx,网上关于这方面的资料一大堆,关于 wordpress, discuz, phpcms, ecshop, shopex 等的 rewrite 应有尽有,直接 copy 就可以。推荐一个在线自动将Apache .htaccess伪静态规则转换为Nginx rewrite网址,具体可以看我博客的常用工具
还有一个 Nginx 新手常见的问题是拿到这些 rewrite 规则后不知道怎么改,比如 Nginx 下子目录的 rewrite 应该改成什么样子?/ 下是 wordpress,/bbs 下装个 discuz,/ 是 discuz,/blog 下装个 wordpress 或者 / 下是 wordpress,/blog 下再装个 wordpress 等,这样的 rewrite 怎么改呢?弄几个例子供参考:
WordPress 安装在子目录 /blog 下:
location /blog/ {
root /home/www/deepvps.com;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^.+/?(/blog/wp-.*) $1 last;
rewrite ^.+/?(/blog/.*.php)$ $1 last;
rewrite ^(.+)$ /blog/index.php?q=$1 last;
}
}
Discuz! 7.2 安装在子目录 /bbs 下:
location /bbs/ {
root /home/www/deepvps.com;
index index.php index.html index.htm;
rewrite ^/bbs/archiver/((fid|tid)-[w-]+.html)$ /bbs/archiver/index.php?$1 last;
rewrite ^/bbs/forum-([0-9]+)-([0-9]+).html$ /bbs/forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /bbs/viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/bbs/space-(username|uid)-(.+).html$ /bbs/space.php?$1=$2 last;
rewrite ^/bbs/tag-(.+).html$ /bbs/tag.php?name=$1 last;
}
Discuz! X1.5 安装在子目录 /bbs 下:
location /bbs/ {
root /home/www/deepvps.com;
index index.php index.html index.htm;
rewrite ^([^.]*)/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^.]*)/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^.]*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^.]*)/thread-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^.]*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^.]*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^.]*)/([a-z]+)-(.+).html$ $1/$2.php?rewrite=$3 last;
if (!-e $request_filename) {
return 404;
}
}
如果对理解 ^([^.]*)/([a-z]+)-(.+).html$ 这样的正则表达式有困难并对这方面有兴趣的话可以看看一些书,最好的一本应该是 O’Reilly 出的 Mastering Regular Expressions(也有中文版:《精通正则表达式》)。
参考:http://www.vpsee.com/2011/04/some-nginx-rewrite-examples-for-subdirectories/


所有时间均为北京时间。现在的时间是 10:41 AM

©2003-2024 1819.net All rights reserved. © 2024