问题概述:

           nginx,咱也来赶赶新潮.给linux服务器配上nginx.

装上WordPress.鉴于WordPress不是太理想的速度.采用了"cos-html-cahce"这一个优秀国产插件. 问题也就来了. "cos-html-cahce"并没正常工作. 现象为只生产首页.文章的html不生成.

排除了目录写权限的因素.还是找不出原因.

解决办法:

      谷歌 百度一番后找到了方法.  导致这个问题的原因是 cos-html-chace没正确获得网站目录的路径.找到问题了解决办法也就迎刃而解了.

1、打开你该网站的 nginx 的 conf 配置文件.  winscp 或者 命令行模式vi都行。

2、找到配置php支持的一段。

        location ~ .*\.(php|php5)?$
            {
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                #fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fcgi.conf;
            }

3、在大括号中间增加粉红色的语句。就是给php指定网站目录了。

    比如你的网站目录为  /home/xxx/public_html  那就加 root /home/xxx/public_html;

    如下是我的配置:

        location ~ .*\.(php|php5)?$
            {

                root  /home/wwwroot/wwekblog;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                #fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fcgi.conf;
            }

测试思考:

         测试结果 cos-html-cache 正常工作. 文章的html文件正常生成.  导致这个故障的原因可能是nginx并没有把网站目录的"属性"传递给fast cgi工作的php. 导致cos-html-cahce没获得正确的网站目录路径.  其实解决这个问题的方法还有一个,就是cos-html-cache官方说明的改这个插件的网站目录获得语句,给它手动指定.这样也可以.  

  

    问题:

在几个blog程序中折腾的结果~ 

导致url连续二次变化。这是第三次了。 

nginx 通过rewrite 使用 “permanent;”参数 成301永久url重定向。 

以往的url结构 

https://www.iamle.com/post/199/ 

现在需要的url结构 

https://www.iamle.com/archives/199.html 

    过程:

学习nginx的配置规则,学习正则表达式(我也没接触过,学呗。) 

nginx的中文维科:http://wiki.nginx.org/NginxChs 

正则表达式入门:http://zh.wikipedia.org/zh-cn/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F 

                http://deerchao.net/tutorials/regex/regex.htm 

    简单说明下(认真学习正则表达式-我没认真学^_^): 

^ 表示 匹配字符串的开始. 

$ 表示 匹配字符串的结束. 

$1 $2 表示变量 

([0-9]+) 表示至少1个、最多不限制的数字串. 

头看晕了(不是程序员。)。写出表达式。 

—————————————————- 

—————————————————- 

[sourcecode language=”css”]
rewrite ^/post/([0-9]+)/$ /archives/$1.html permanent;
rewrite ^/html/y2009/([0-9]+).html$ /archives/$1.html permanent;
[/sourcecode]

  

把表达式加入nginx conf 文件。 

—————————————————– 

location / {  

放这里。 

—————————————————- 

       完成配置:

这是我的完整的WordPress nginx rewrite 规则配置。 

1、nginx rewrite网址url变更301重定向。 

2、nginx rewrite iamle.cn 重定向到 www.iamle.com 。 

3、nginx WordPress rewrite伪静态规则(得以支持WordPress的自定义url)。 

[sourcecode language=”php”]
location / {
                  rewrite ^/post/([0-9]+)/$ /archives/$1.html permanent;
                  rewrite ^/html/y2009/([0-9]+).html$ /archives/$1.html permanent;
            if ($host !~ "^www\.iamle\.com$"){
                  rewrite ^(.*)  https://www.iamle.com$1 permanent;
                                             }
            if (-f $request_filename/index.html){
                  rewrite (.*) $1/index.html break;
                                                }
            if (-f $request_filename/index.php){
                  rewrite (.*) $1/index.php;
                                                }
            if (!-f $request_filename){
                  rewrite (.*) /index.php;
                                      }
                    } 
[/sourcecode]

  

        测试结果:

访问:https://www.iamle.com/post/199/ 即可被跳转到 https://www.iamle.com/archives/199.html