Nginx php-fpm fast-cgi 502 Bad Gateway错误处理

  •   2009-07-31/22:47
  • Nginx php-fpm fast-cgi 502 Bad Gateway错误是FastCGI有问题,造成NGINX 502错误的可能性比较多。
    将网上找到的一些和502 Bad Gateway错误有关的问题和排查方法列一下,先从FastCGI配置入手:

    1.FastCGI进程是否已经启动

    2.FastCGI worker进程数是否不够
    通过命令查看服务器上一共开了多少的 php-cgi 进程
          ps -fe |grep "php" | grep -v "grep" | wc -l
    使用如下命令查看已经有多少个php-cgi进程用来处理tcp请求
        netstat -anop | grep "php" | grep -v "grep" | wc -l
    接近配置文件中设置的数值,表明worker进程数设置太少
    参见:

    3.FastCGI执行时间过长
    根据实际情况调高以下参数值
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    4.FastCGI Buffer不够
    nginx和apache一样,有前端缓冲限制,可以调整缓冲参数
    fastcgi_buffer_size 32k;
    fastcgi_buffers 8 32k;
    参见:

    5.Proxy Buffer不够
    如果你用了Proxying,调整
    proxy_buffer_size  16k;
    proxy_buffers      4 16k;
    参见:

    6.https转发配置错误
    正确的配置方法

    server_name ;
    location /myproj/repos {
    set $fixed_destination $http_destination;
    if ( $http_destination ~* ^https(.*)$ )
    {
    set $fixed_destination http$1;
    }
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Destination $fixed_destination;
    proxy_pass ;
    }
    参见:

    7.查看php-fpm.log
    错误请参看:

    8.php的bug
    请参看:

    9.php-fpm.conf的配置
    请参看

    10.nginx.conf的rewrite-url规则等

    11.php-fpm.pid
    鄙人在/php/sbin/php-fpm 里面把
    php_fpm_PID=/data1/php/logs/php-fpm.pid修改成
    #php_fpm_PID=/data1/php/logs/php-fpm.pid
    故猜想是用户权限的问题已php-fpm.conf里的用户启动该服务估计问题会消失

    另外nginx 400 bad request 错误的原因和解决办法
    在nginx.conf中,将client_header_buffer_size和large_client_header_buffers都调大,可缓解此问题。
    其中主要配置是client_header_buffer_size这一项,默认是1k,所以header小于1k的话是不会出现问题的。
    按我现在配置是:
    client_header_buffer_size 16k;
    large_client_header_buffers 4 64k;
    这个配置可接收16k以下的header,在浏览器中cookie的字节数上限会非常大,所以实在是不好去使用那最大值。
    本文转载自『北漂石头的博客』

    更多精彩内容,欢迎访问北漂石头的博客!


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多