• WordPress持久链接permalink的Nginx rewrite规则

    Posted on 二月 22nd, 2009 inetdemon 6 comments

    我用wordpress super cache和nginx来加速wp博客网页,由于需要使用wordpress permalink,需要为wordpress建立一个nginx的rewrite规则, 参照了wordpress super cache作者博客中的一篇评论,具体操作方法如下,首先新建一个文件your_nginx_path/conf/wp_rewrite.conf, 建立如下:

    if (-f $request_filename) {
    expires 7d;
     break;
     }
     
     set $supercache_file ”;
     set $supercache_uri $request_uri;
     
     if ($request_method = POST) {
     set $supercache_uri ”;
     }
     
     # Using pretty permalinks, so bypass the cache for any query string
     if ($query_string) {
     set $supercache_uri ”;
     }
     
     if ($http_cookie ~* “comment_author_|wordpress|wp-postpass_” ) {
     set $supercache_uri ”;
     }
     
     # if we haven’t bypassed the cache, specify our supercache file
     if ($supercache_uri ~ ^(.+)$) {
     set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
     }
     
     # only rewrite to the supercache file if it actually exists
     if (-f $document_root$supercache_file) {
     rewrite ^(.*)$ $supercache_file break;
     }
     
     # all other requests go to WordPress
    if (!-e $request_filename) {
    rewrite . /index.php last;
    }
    然后在你的nginx配置文件中include这个文件,重新load配置即可。

     

    6 responses to “WordPress持久链接permalink的Nginx rewrite规则”

    1. 现在速度好像还可以。
      写的不错,坚持!

    2. 谢谢鼓励,我会加油的:)

    3. Hi,

      我安装了Wordpress v2.7.1 和 Nginx 在我的Windows Home Vista 电脑上。我设置wordpress使用nice permalinks 如 %postname%. 但是,这么一来,所有的post的URL就都不能操作了。

      请问你可以帮我吗?可否提供一个Nginx 的 Rewrite 规则,就你如以上所写的,可以放在 wp_rewrite.conf

      谢谢你!

    4. Hi, 我这篇博客也是用的这个nginx的rewrite规则,用的permalink,是可以用的。你如果有问题,可以把最后那段改成下面代码试试:
      if (!-e $request_filename) {
      rewrite ^(.+)$ /index.php?q=$1 last;
      }

    5. 谢谢你的回复!
      我试了,还是不行。真的很奇怪!
      我在网上搜索了很久,答案与你的很相似,也不知道问题出在哪里.
      我对于PHP是一窍不通的。但可以大概猜得懂。我把情况告诉你详细一点,以你这方面的专才,也许你可以帮得了我。

      我现在什么plugins都没有添加。所以,我没有用你的wp_rewrite.conf文件。我是直接把你的code放到nginx.conf 里头。如下:
      location / {
      root /nginx/html;
      index index.php index.html index.htm;

      if (!-e $request_filename) {
      rewrite ^(.+)$ /index.php?q=$1 last;
      }
      }

      为了找出问题所在,我目前电脑上的Wordpress blog是很简单的,没有plugin,只有几个posts,用的是default 的theme.

      当我的permalink setting 是default的时候,一切操作正常。但我的permalink setting改为%postname%时,主页还是看得见,一旦点击任何page,post,category,便会出现404的error了。

      permalink 改为%postname@时,当鼠标移到任何post的title时,internet browser IE 的status bar上显示的是 http://…./blog/my-first-post 我知道这是对的。

      但奇怪的是,当鼠标移到任何一个category或tag上面时,显示的却是:
      http://…../blog/category/holiday-photos
      http://…../blog/tag/happy-hour

      网址上的”category”和”tag”本不应该出现的,不知怎么冒出来。也许,你可否从这看出什么来?

      若有必要,我可以把整个nginx.conf文件email给你。希望你可以帮我解决这个头痛的nginx rewrite 的问题。

      我是根据以下网址的instructions来安装nginx和wordpress的:
      blogbuildingu.com/wordpress/install-wordpress-wemp

      非常谢谢你了!

    6. 你说的category和tag是wordpress pretty permalink中的默认设置,并没有什么问题,如果需要的话你可以在wordpress后台管理节面中“固定链接”的下方修改这两个字符串。
      建议你有空还是看一下nginx rewrite规则设定详细介绍(中文)。从你的设定来看,你的博客根目录为/blog/,那么你就不能rewrite到根目录了,nginx的rewrite规则可以相应改为:
      location /blog/ {
      if (!-e $request_filename) {
      rewrite ^(.+)$ /blog/index.php?q=$1 last;
      }
      }
      修改设定后如果还有错误,需要检查以下几点:
      1. 查看服务器的rewrite模块是否已经打开
      2. 检查wordpress的安装和你的规则是否相符,你可以直接访问yourwebsite.com/index.php?q=my-first-post
      3. 如果以上两个方法还是不行,需要查看你的服务器的errorlog或者accesslog,看看web服务器或者模块是否有安装问题

    Leave a reply