这几天在配置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解析
基础教程推荐
- Yii框架连表查询操作示例 2023-02-13
- PHP实现抽奖系统的示例代码 2023-06-26
- php实现数组筛选奇数和偶数示例 2024-02-05
- php实现构建排除当前元素的乘积数组方法 2022-11-23
- PHP手机短信验证码实现流程详解 2022-10-18
- 设定php简写功能的方法 2023-03-17
- php数组函数序列之array_sum() – 计算数组元素值之和 2024-01-15
- PHP实现文件下载【实例分享】 2024-04-27
- PHP+MySQL+sphinx+scws实现全文检索功能详解 2023-01-31
- PHP判断一个字符串是否是回文字符串的方法 2024-01-31
