« MySQL优化之COUNT(*)效率 MooPHP beta版本更新小计 »

Nginx下Discuz!的Rewrite

在上篇《Nginx的Rewrite配置》之后,有不少朋友搜索过来询问相关的设置,于是整理了下,发出来给大家共享。

突然间发现自己写的几篇日志都是关于Nginx
Rewrite配置,感兴趣的同学可以查看下。

Discuz!在Nginx下的Rewrite
需要说明的是,下网上以前一直流传的Rewrite都是有误的。
下面的Rewrite中百分号前面多了个转移字符“\”,这在Apache中是需要的,而在Nginx中则是不需要的。
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
正确的应该是
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
这个错误在基本上目前所有使用Nginx作为服务器,并且开启了Rewrite的网站上存在。包括Discuz!官方,目前已经给cnteacher反馈了。

Nginx实例代码
server {
listen 80;
server_name www.ccvita.com ccvita.com;

location / {
index index.html index.htm index.php;
root /www/www.ccvita.com;
rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

}

location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8694;
fastcgi_param SCRIPT_FILENAME /www/www.ccvita.com$fastcgi_script_name;
}

location /www.ccvita.com-status {
stub_status on;
access_log off;
}
}

相关文档
Nginx下Discuz!的Rewrite》:http://www.ccvita.com/348.html
Nginx下WordPress的Rewrite》:http://www.ccvita.com/336.html
Nginx的Rewrite配置》:http://www.ccvita.com/319.html
Nginx的防盗链配置》:http://www.ccvita.com/312.html

“Nginx下Discuz!的Rewrite”

14条回复
  1. 实际运行的Nginx下Discuz!的Rewrite。

  2. 博主按你的方法还是不能成功:nginx+discuz!还会不能正常显示,下面是我的default配置:

    server {
    listen 80;
    server_name yl93.2288.org;
    location / {
    index index.php index.html index.htm;
    root /var/www;
    rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
    rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
    rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
    rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
    rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {

    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #proxy_pass http://127.0.0.1;
    #}

    #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:8694;
    fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
    }
    location /yl93.2288.org-status {
    stub_status on;
    access_log off;
    }
    # deny access to .htaccess files, if Apache’s document root
    # concurs with nginx’s one
    #
    #location ~ /\.ht {
    #deny all;
    #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #listen 8000;
    #listen somename:8080;
    #server_name somename alias another.alias;

    #location / {
    #root html;
    #index index.html index.htm;
    #}
    #}

    # HTTPS server
    #
    #server {
    #listen 443;
    #server_name localhost;

    #ssl on;
    #ssl_certificate cert.pem;
    #ssl_certificate_key cert.key;

    #ssl_session_timeout 5m;

    #ssl_protocols SSLv2 SSLv3 TLSv1;
    #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #ssl_prefer_server_ciphers on;

    #location / {
    #root html;
    #index index.html index.htm;
    #}
    #}

    请帮忙看一下

  3. 回楼上的,不少地方需要按照实际情况改动的,比如
    fastcgi_pass 127.0.0.1:8694
    这里

    我推荐你看张宴的关于Nginx的教程,很详细
    http://www.s135.com/read.php/366.htm

  4. rewrite “^(.*)/date/([0-9]{6})/([0-9]+)/$” /index.php?action=index&setdate=$1&page=$2 last;
    rewrite ^(.*)/page/([0-9]+)/$ index.php?page=$1 last;
    rewrite ^(.*)/category/([0-9]+)/([0-9]+)/$ /index.php?cid=$1&page=$2 last;
    rewrite ^(.*)/category/(.+)/([0-9]+)/$ /index.php?curl=$1&page=$2 last;
    rewrite ^(.*)/(archives|search|reg|login|index|links|aboutus)/$ /index.php?action=$1 last;
    rewrite ^(.*)/(comments|tagslist|index)/([0-9]+)/$ /index.php?action=$1&page=$2 last;
    rewrite ^(.*)/tag/(.+)/([0-9]+)/$ /index.php?action=tags&item=$1&page=$2 last;
    rewrite ^(.*)/archives/([0-9]+)/([0-9]+)/$ /index.php?action=show&id=$1&page=$2 last;
    rewrite ^(.*)/(.+)/([0-9]+)/$ /index.php?action=show&alias=$1&page=$2 last;
    rewrite ^(.*)/user/(.+)/$ /index.php?action=finduser&user=$1 last;
    rewrite ^(.*)/uid/([0-9]+)/$ /index.php?action=finduser&uid=$1 last;

    帮我看看有什么问题吗?总是不行。

  5. 大哥是高手,不知道能不能提供一下joomla的nginx rewrite规则。很多idc都不能解决这个问题,其中说有6年运营经验的华夏名网,他们所有的技术都不能写出joomla的nginx rewrite规则。

    @phoenix, 你把joomla在apache下的.htaccess文件帖出来,我来看看

  6. 规则的地址是:http://www.jishunxiang.com/htaccess.txt
    我google了一下,有个老外写出来规则,但国内同行说不能用。贴出来您参考一下。
    if ( $args ~ “mosConfig_[a-zA-Z_]{1,21}(=|\%3d)” ) {
    set $args “”;
    rewrite ^.*$ http://host/index.php last;
    return 403;
    }

    if ( $args ~ “base64_encode.*\(.*\)” ) {

    set $args “”;
    rewrite ^.*$ http://host/index.php last;
    return 403;
    }

    if ( $args ~ “(\|%3E)” ) {
    set $args “”;
    rewrite ^.*$ http://host/index.php last;
    return 403;

    }
    if ( $args ~ “GLOBALS(=|\[|\%[0-9A-Z]{0,2})” ) {
    set $args “”;
    rewrite ^.*$ http://host/index.php last;
    return 403;

    }

    if ( $args ~ “_REQUEST(=|\[|\%[0-9A-Z]{0,2})” ) {
    set $args “”;
    rewrite ^.*$ http://host/index.php last;
    return 403;

    }

  7. 兄弟joomla的nginx 规则有进展吗?全国人民都在等待你胜利的消息。

  8. joomla的nginx重写网上就有,你可以参照这篇http://www.jefflei.com/post/1417.html

  9. Fantastic thank you. Find the files I are looking for!

  10. 博主,你好,我刚接触nginx,用了半个月了,一直尝试discuz的rewrite,一直用不了,能否加我58219960,帮我一下。。实在搞不定,唉。

    @两耳失聪, 这里的是可用的啊~~你按照文章中的去写就可以了。

  11. 博主,我研究程序和网站,有7年多了,不是菜鸟,如果不是,实在解决不了,我真的没必要找你。

    麻烦能否抽出10分钟,帮我看看,头疼坏了。可能是因为我从Win服务器,到现在开始用linux的缘故。。

    @两耳失聪, 已经邮件你联系方式了 :)

回复留言

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

你可以使用XHTML标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>