Nginx 502错误:connect() failed (111: Connection refused) while connecting to upstream
今天在CentOS8部署lemp环境部署完毕后提示502 Bad Gateway
错误,查看/var/log/nginx/error.log
日志发现错误内容:
2020/12/16 18:14:45 [error] 5574#0: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 49.72.84.213, server: towait.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.towait.com"
第一反应是php-fpm
连接失败,于是查看文件/etc/php-fpm.d/www.conf
中的端口监听参数写的是
listen = /run/php-fpm/www.sock
而nginx.conf
文件中fastcgi_pass
参数后面写的是127.0.0.1:9000
,于是修改一下:
fastcgi_pass unix:/run/php-fpm/www.sock;
重启nginx,问题解决。
问题补充
1.访问php文件会直接被下载
这是由于缺失php脚本解析的配置,在server{...}配置区域中加入下面参数即可
location ~ \.php$ { fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
这是一份完整的nginx文件参数
server { listen 80; server_name sipmv.com www.sipmv.com; root /mnt/www/sipmv.com/public_html/; index index.php index.html index.htm; access_log /var/log/nginx/sipmv.com.access.log; error_log /var/log/nginx/sipmv.com.error.log; location ~ \.php$ { fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }