Nginx + PHP-FPM = "Random" 502 Bad Gateway

7 nginx fastcgi php-fpm 502-error

我正在运行 Nginx 并通过 FastCGI 将 php 请求代理到 PHP-FPM 进行处理。我会随机收到 502 Bad Gateway 错误页面 - 我可以通过非常快速地点击我的 PHP 网站/刷新页面一两分钟来重现这个问题。当我收到 502 错误页面时,我所要做的就是刷新浏览器并且页面正确刷新。

这是我的设置:

nginx/0.7.64 PHP 5.3.2 (fpm-fcgi)(构建时间:2010 年 4 月 1 日 06:42:04)Ubuntu 9.10(最新 2.6 Paravirt)

我使用这个 ./configure 指令编译了 PHP-FPM

./configure --enable-fpm --sysconfdir=/etc/php5/conf.d --with-config-file-path=/etc/php5/conf.d/php.ini --with-zlib --with -openssl --enable-zip --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-soap --enable-sockets --disable-cgi --with-curl --with -curlwrappers --with-gd --with-mcrypt --enable-memcache --with-mhash --with-jpeg-dir=/usr/local/lib --with-mysql=/usr/bin/mysql -- with-mysqli=/usr/bin/mysql_config --enable-pdo --with-pdo-mysql=/usr/bin/mysql --with-pdo-sqlite --with-pspell --with-snmp --with- sqlite --with-tidy --with-xmlrpc --with-xsl

我的 php-fpm.conf 看起来像这样(相关部分):

 ...   
<value name="pm">
     <value name="max_children">3</value>
     ...
     <value name="request_terminate_timeout">60s</value>
       <value name="request_slowlog_timeout">30s</value>
       <value name="slowlog">/var/log/php-fpm.log.slow</value>
       <value name="rlimit_files">1024</value>
       <value name="rlimit_core">0</value>
       <value name="chroot"></value>
       <value name="chdir"></value>
       <value name="catch_workers_output">yes</value>
       <value name="max_requests">500</value>
...
Run Code Online (Sandbox Code Playgroud)

我试过将 max_children 增加到 10 并没有什么区别。我还尝试将其设置为“动态”并将 max_children 设置为 50,将 start_server 设置为“5”,没有任何区别。

我曾尝试同时使用 1 和 5 个 nginx 工作进程。

我的 fastcgi_params conf 看起来像:

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
fastcgi_param  REDIRECT_STATUS    200;
Run Code Online (Sandbox Code Playgroud)

Nginx 将错误记录为:

[错误] 3947#0:*10530 连接()失败(111:连接被拒绝),同时连接到上游,客户端:68.40.xxx.xxx,服务器:www.domain.com,请求:“GET /favicon.ico HTTP/ 1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“www.domain.com”

PHP-FPM 在错误发生时记录以下内容:

[NOTICE] pid 17161, fpm_unix_init_main(), line 255: getrlimit(nofile): max:1024, cur:1024 
[NOTICE] pid 17161, fpm_event_init_main(), line 93: libevent: using epoll 
[NOTICE] pid 17161, fpm_init(), line 50: fpm is    running, pid 17161 
[DEBUG] pid 17161, fpm_children_make(), line 403: [pool default] child 17162 started 
[DEBUG] pid 17161, fpm_children_make(), line   403: [pool default] child 17163    started 
[DEBUG] pid 17161,  fpm_children_make(), line 403: [pool default] child 17164 started 
[NOTICE] pid 17161, fpm_event_loop(), line 111: ready to handle connections
Run Code Online (Sandbox Code Playgroud)

当我重新创建问题时,我的 CPU 使用率最高约为 10-15%。我的免费内存 (free -m) > 130MB

我在使用 php5-cgi 为我的 php 请求提供服务时也遇到了这个间歇性的 502 Bad Gateway 问题。有谁知道如何解决这一问题?

编辑/更新:我正在使用主管来启动 php-fpm(因此没有被取消)。

Sav*_*btz 2

我觉得你的max_children水平太低了

<value name="max_children">3</value>
Run Code Online (Sandbox Code Playgroud)

您也可以尝试将 php-fpm 从 TCP 端口切换到 unix 套接字:

php-fpm.conf

<value name="listen_address">/tmp/php.sock</value>
Run Code Online (Sandbox Code Playgroud)

nginx.conf

fastcgi_pass unix:/tmp/php.sock;
Run Code Online (Sandbox Code Playgroud)

详细说明:

  1. Unix 套接字快约 20%
  2. 您不为每个连接使用时间等待套接字

  • 只是给提出类似问题的人提供了相反的建议:) (3认同)