前言:
使用宝塔安装的php,nginx环境,因为想搭一个使用ip加端口访问的项目,所以没有使用他自带的一键搭建网站,就自己整了一下,结果项目可以跑访问html页面正常,但是访问php 报错 502 Bad Gateway
值得注意的是他的nginx.加载文件目录是/www/server/panel/vhost/nginx,而不是/www/server/nginx/conf/vhost,这个可以看nginx.conf的最下面的加载代码
include /www/server/panel/vhost/nginx/*.conf;
nginx配置如下
server {
listen 8180;
root /www/wwwroot/kx2/;
server_name localhost;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
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;
}
}
可以看到我也使用监听9000端口的方式配置的,但当我排查问题发现找不到9000端口
最终查到 php-fpm.conf 的配置
至此,我们得出一个结论,php-fpm的监听 跟nginx配置的监听方式差异造成的访问php文件报错502 ,所以修改方式有两种,一种是修改php-fpm 监听方式为 127.0.0.1:9000
另一种方式就像下面的修改nginx的 fastcgi_pass 为 unix:/tmp/php-cgi-70.sock;
server {
listen 8180;
root /www/wwwroot/kx2/;
server_name localhost;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php(.*)$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-cgi-70.sock;
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;
}
}
总结:当我们遇到nginx环境访问项目正常,但是访问php文件报错502 Bad Gateway 时可以从 listen监听方式是否一致去排查
分享几篇文章