Nginx 502 错误网关

Nginx 502 错误网关

我的 Centos 6.8 服务器上当前出现 502 错误网关错误。

    2017/01/30 23:57:31 [crit] 26911#0: *1 connect() to unix:/var/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.15, server: 192.168.0.$

我检查了该目录是否存在,但不存在。我能找到的最接近该文件夹的是 /var/run/php-fpm,其中仅包含 php-fpm.pid。

答案1

以下是解决此问题的步骤:

  1. 检查是否php-fpm正在运行:

    sudo service php-fpm status
    sudo service php7-php-fpm status # use this if you are using remi PHP 7
    
  2. 检查listenphp-fpm 的 www.conf 配置文件中的指令:

    grep -Ri listen /etc/php/7.0/fpm/pool.d/www.conf 
    grep -Ri listen /etc/opt/remi/php70/php-fpm.d # for remi
    
  3. 确保您的 nginxfastcgi_pass指令与您的listen指令相匹配。

答案2

对于我来说,安装过程如下,在从nginx.org 仓库

mkdir -p /var/www/public_html
chown -R nginx:nginx /var/www/public_html
restorecon -rv /var/www
yum install php-fpm
sed -i 's@listen = 127.0.0.1:9000@listen = /var/run/php-fpm/php-fpm.sock@' /etc/php-fpm.d/www.conf
sed -i 's@;listen.owner = [email protected] = nobody@' /etc/php-fpm.d/www.conf
sed -i 's@;listen.group = [email protected] = nobody@' /etc/php-fpm.d/www.conf
sed -i 's@user = apache@user = nginx@' /etc/php-fpm.d/www.conf
sed -i 's@group = apache@group = nginx@' /etc/php-fpm.d/www.conf
systemctl start php-fpm.service
systemctl enable php-fpm.service

那么你的主机配置应该是这样的:

server
{
    listen 443 ssl;

    root /var/www/public_html;
    index index.php;

    location ~ \.php$
    {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

然后重新启动 nginx 就可以了。

相关内容