背景
使用 nginx 配置 index.php 路径;配置完 nginx,访问页面报错。
nginx 配置如下:
server {
listen 80;
server_name xxx.com;
root xxx/xxx/xxx;
location ~ .php(.*)$ {
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
排查路径
排查php服务是否打开,端口是否正确
-
使用
ps -ef | grep php-fpm查看php服务是否启动;成功启动:
没有启动:
-
使用
netstat -lntp | grep php查看端口号可以看出这是在 9000 端口;
修改 nginx 配置文件
fastcgi_pass 127.0.0.1:9002;为fastcgi_pass 127.0.0.1:9000;
造成 nginx 403的原因主要有如下两点:
- nginx 没有权限访问 "xxx/xxx/xxx"
- nginx 找不到 "xxx/xxx/xxx"
- 没有指定 index,导致 nginx 找不到文件
核对以上几点:
- 确认 nginx 有访问 "xxx/xxx/xxx" 的权限,没有使用
chmod修改权限 - 确认 "xxx/xxx/xxx" 有 index 文件
- 修改 nginx 配置: 添加
index index.php index.html;
最后 nginx 配置文件如下
server {
listen 80;
server_name test.com;
root xxx/xxx/xxx;
index index.php index.html;
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
docker 配置 php
由于本人是使用 docker 搭建的 php 服务,docker-compose.yml 为如下:
services:
php1:
image: php7.1-fpm:1.0
ports:
- "9002:9000"
container_name: "php-project"
volumes:
- xxx:xxx
nginx 配置如下
server {
listen 80;
server_name test.com;
root xxx/xxx/xxx;
index index.php index.html;
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
这里有个小问题是,docker在文件映射的过程中,有一个文件夹内容映射失败了。比如宿主机中存在 xxx/xxx/xxx/index.php 但是,进入容器中只有 xxx/xxx/xxx 目录; 没有xxx/xxx/xxx/index.php 文件,导致访问该接口 403;
进行的处理:
mv xxx/xxx/xxx xxx/xxx/xxa;cp -r xxx/xxx/xxa xxx/xxx/xxx;