这几天在配置Nginx,PHP用FastCGI,在Apache里,有alias,比较方便,在Nginx下没有虚拟目录概念的,是用location配合alias使用,但使用alias标签的目录块中不能使用rewrite的break。一、例子说明:1)我的环境是:we...
                
这几天在配置Nginx,PHP用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指令指定的路径。
本文标题为:配置nginx下别名alias支持PHP fastcgi解析
				
        
 
            
        - laravel实现按月或天或小时统计mysql数据的方法 2023-02-22
 - php微信公众号开发之秒杀 2022-11-23
 - 用nohup命令实现PHP的多进程 2023-09-02
 - Laravel balde模板文件中判断数据为空方法 2023-08-30
 - windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
 - PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
 - PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
 - PHP中PDO事务处理操作示例 2022-10-15
 - PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
 - laravel通用化的CURD的实现 2023-03-17
 
						
						
						
						
						
				
				
				
				