www下面有个index.html,有个aaa.php
index.html是生成的静态,且后台可以控制其是否存在(“关闭生成静态”功能可以删掉该文件)。
需求:
如果index.html存在,就直接访问index.html。
如果不存在,仍然访问index.html,只不过这个时候是通过配置的伪静态了:
rewrite  ^/([a-zA-Z]+)\.html$   /aaa.php?controller=$1&action=$1 last;其虚拟机配置如下:server {
listen  80;
server_name www.my.com; location / {
root  D:/www;
index  index.html;
include  fastcgi_params;
rewrite  ^/([a-zA-Z]+)\.html$ /aaa.php?controller=$1&action=$1  last;
} location ~ \.php$ {
root  D:/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME  D:/www$fastcgi_script_name;
include  fastcgi_params;
}
}这个配置,当index.html存在时,无论访问www.my.com还是访问www.my.com/index.html,都只会通过伪静态访问,而不会直接访问已经存在的index.html。
当index.html不存在时,访问www.my.com报403错误,访问www.my.com/index.html时正常通过伪静态请求。求助,谢谢了。nginx 伪静态

解决方案 »

  1.   

    if (!-f $request_filename) { 
    rewrite  ^/(([a-zA-Z]+)\.html)?$    /aaa.php?controller=$1&action=$1  last;
    break;
    }
    你提到的如果index.html不存在才能重写,那么就用这么一个判断 -f表示文件存在 !-f即不存在
    另外用 / 方式访问,就需要改一下转发条件。允许将请求 / 转发。
    另外关于参数的条件$1,可能 / 请求下会为空,所以还需要你的应用做一下缺省判断
      

  2.   

    楼上说的对,加个if(!-f $request_filename),再去调用你的rewrite就可以了。
      

  3.   


    忘记说了,这办法也试过,甚至 if (!-f D:/www)  都这样写了,仍然无效不信你可以建个www目录试试,aaa.php入口随便echo个什么东东都行。
      

  4.   

    注意到我改了转发规则吗?
    ^/([a-zA-Z]+)\.html$   >>>  ^/(([a-zA-Z]+)\.html)?$
      

  5.   


    谢谢dream1206  我还以为是你写错了的  就没按照你的配置
    说过的给加分  加了     再次感谢