微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

配置nginx下别名alias支持PHP fastcgi解析

这几天在配置NginxPHP用FastCGI,在Apache里,有alias,比较方便,在Nginx下没有虚拟目录概念的,是用location配合alias使用,但使用alias标签的目录块中不能使用rewrite的break。

一、例子说明:

1)我的环境是:web根目录在 /var/www/html/中,但是我要加上一个类似于apache的别名目录 /bbs ,此目录不在 web根目录中。
我的配置文件如下:

server { 
    listen       80;
    server_name    localhost;
    default_type text/plain;
    location / {
        root    /var/www/html;
        index    index.PHP index.htm index.html;
    }
    location /bbs {
        alias /opt/bbs;
        index index.html index.htm index.PHP;
    }
    location ~ ^/bbs/.+\.PHP$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param SCRIPT_FILENAME /opt$fastcgi_script_name;
        include        fastcgi_params;
        #include fastcgi.conf;
    }
  location ~ \.PHP$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
        #include fastcgi.conf;
    }
}

说明: 上面这个就是成功的例子。

二、但是又如下几点需要注意:

1)location ~ \.PHP$ {}  段,必须放在 location ~ ^/bbs/.+\.PHP$ {} 段后面,否则/bbs/的url打不开
2) location ~ ^/bbs/.+\.PHP$ {} 里面也可以写成如下:

location ~ ^/bbs/.+\.PHP$ {
        root /opt;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

也就是用变量名 $document_root 代替 /opt; 其实每个 location {}中的 $document_root 都是局部变量,都是在本段配置 root指令指定的路径。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐