Nginx下Discuz!的Rewrite
本文最后更新于 5673 天前,其中的信息可能已经有所发展或是发生改变。

在上篇《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》:https://kimi.pub/348.html
Nginx下WordPress的Rewrite》:https://kimi.pub/336.html
Nginx的Rewrite配置》:https://kimi.pub/319.html
Nginx的防盗链配置》:https://kimi.pub/312.html

评论

  1. 博主
    16年前
    2008-9-06 2:16:24

    实际运行的Nginx下Discuz!的Rewrite。

  2. 16年前
    2008-9-21 16:34:13

    博主按你的方法还是不能成功: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. 博主
    16年前
    2008-9-21 16:54:26

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

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

  4. kiki
    15年前
    2009-3-05 13:50:48

    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. 15年前
    2009-4-10 16:04:34

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

    • 博主
      phoenix
      15年前
      2009-4-10 16:16:21

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

  6. 15年前
    2009-4-11 1:23:48

    规则的地址是: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. 15年前
    2009-4-17 12:32:15

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

  8. inetdemon
    15年前
    2009-5-13 22:32:53

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

  9. Nataxa
    15年前
    2009-5-20 20:07:35

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

  10. 14年前
    2009-12-20 16:36:19

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

    • 博主
      两耳失聪
      14年前
      2009-12-20 22:06:37

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

  11. 14年前
    2009-12-20 22:55:01

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

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

    • 博主
      两耳失聪
      14年前
      2009-12-20 23:41:14

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

  12. 13年前
    2011-7-03 12:26:52

    已经收藏经典啊

  13. 长风
    12年前
    2011-10-20 15:10:13

    如何配置nginx与tomcat的整合啊?

  14. guest
    12年前
    2012-8-21 20:01:38

    兄弟,你的nginx rewrite规则是错的!!!!

  15. 11年前
    2013-4-16 14:56:36

    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]+)-([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 ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
    rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
    if (!-e $request_filename) {
    return 404;
    }

  16. 9年前
    2015-5-05 3:45:57

    感谢博主,我搞定了!

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇