为nginx设置wordpress插件supercache的rewrite规则

Wordpress的Supercache插件相信玩wordpress的兄弟都知道,它通过生成静态页面并压缩输出来提升访问速度。不过插件作者只制作了基于apache虚拟主机的url rewrite规则,对于现在很多用nginx主机的人来说,就需要自己来写规则了。今天大致研究了一下,得出了一个我自己的方案,可以实现nginx支持supercache的重写。

要实现这个功能,你的nginx必须编译了pcre支持(用来支持url rewrite),以及gzip支持(对应编译参数 –with-http_gzip_static_module),否则下面的一切都是空谈。

下面这一段配置,你可以加入到你需要进行设置的虚拟主机配置文件中。注意以下几点:

  • location / :由于我的wordpress是直接运行在域名根目录下,所以这里就是/,如果你的目录运行在域名的子文件夹(如/blog/)下,那么这里就要变为 location /blog/ , 同时文件中间/wp-content/ 那一部分也需要改成 /blog/wp-content/
  • 代码段中最后两个if块,是让nginx支持url rewrite的,注意不要写重复了
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    
            location /
            {
                    autoindex off;
                    gzip_static on;
     
                    set $wp_super_cache_file '';
                    set $wp_super_cache_uri $request_uri;
     
                    if ( $request_method = POST )
                    {
                            set $wp_super_cache_uri '';
                    }
     
                    if ( $query_string )
                    {
                            set $wp_super_cache_uri '';
                    }
     
                    if ( $http_cookie ~* "comment_author_|wordpress|wp-postpass_" )
                    {
                            set $wp_super_cache_uri '';
                    }
     
                    if ( $wp_super_cache_uri ~ ^(.+)$ )
                    {
                            set $wp_super_cache_file /wp-content/cache/wp_super_cache/$http_host/$1index.html;
                    }
     
                    if ( -f $document_root$wp_super_cache_file )
                    {
                            rewrite ^(.*)$ $wp_super_cache_file break;
                    }
     
                    if (-f $request_filename)
                    {
                            expires 30d;
                            break;
                    }
     
                    if (!-e $request_filename)
                    {
                            rewrite ^(.+)$ /index.php?q=$1 last;
                    }
            }

    下载文件:

      nginx-wordpress-supercache (1.4 KiB, 72 hits)
    You need to be a registered user to download this file.

    分享 :
    • Digg
    • Sphinn
    • del.icio.us
    • Facebook
    • Mixx
    • QQ书签
    • Google Bookmarks
    • Live
    • Twitter
    • 豆瓣
    • 豆瓣九点
    Categories: linux - wordpress
    不过我的评论提交以后还是不显示的,不知道这个怎么做?那个Lock Down我关闭的……
    3 五月 09 at 06:42
    gzip_static on;


    因为这句话,都不能启动Nginx了,删除了就没问题
    3 五月 09 at 16:28
    admin
    应该是你的nginx没有编译gzip相关的支持,才导致了启用gzip_static on不能启动nginx
    3 五月 09 at 20:47
    额……那要怎么做?不过从页面分析来看,应该是已经自动gzip了……我是加在全局的,不是一个网站一个网站加的
    6 五月 09 at 19:54
    admin
    那你把gzip_static那一句去掉看看是什么状况?
    6 五月 09 at 23:08
    去掉以后,可以正常使用,因为在http {那里已经加入了 gzip on;

    但是,发表一个评论以后,不会自动更新缓存 郁闷的说 博主怎么联系你?有QQ或者Gtalk之类的么?
    8 五月 09 at 14:31
    我的是wp-super cache不在cache文件里写任何内容,除了一个blogs文件夹和.htaccess文件外,没有任何其它文件
    17 九月 09 at 01:04