nginx下为wordpress配置重写规则/rewrite

nginx不能支持.htaccess的rewrite规则,所以使用nginx作web服务器,配置wordpress固定链接会比较麻烦一点,要自己写规则到nginx配置文件里。网上也看到一些配置规则,不过觉得怪怪的。

发现nginx官方网站上有个页面,关于wordpress重写规则的,http://wiki.nginx.org/WordPress里面有“cool”的规则,本站的nginx配置如下,其中一行 try_files $uri $uri/ /index.php; 就是这行,应该比网上有些文章里的规则更cool。

    server {
        server_name  blog.path8.net;
        root /var/www/vhost/blog;
        access_log  logs/vhost-blog.path8.net.log  main;

        location /favicon.ico {
                log_not_found off;
                access_log off;
        }
        location /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                index  index.html index.htm index.php;
                # from nginx.org rewrite rule
                # This is cool because no php is touched for static 
 try_files $uri $uri/ /index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/vhost/blog$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

nginx下为wordpress配置重写规则/rewrite》上有2个想法

    • 具体可以看nginx官方手册。笼统的可以从字面理解,尝试文件, try_files $uri $uri/ /index.php 也就是尝试文件$uri 是否存在,如果存在,就使用$uri处理请求,否则使用 /index.php处理请求。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据