Nginx 和 PHP-FPM 连接耗尽

Nginx 和 PHP-FPM 连接耗尽

我经常遇到这样的错误,

[02-Jun-2012 01:52:04] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 19 idle, and 49 total children
[02-Jun-2012 01:52:05] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 19 idle, and 50 total children
[02-Jun-2012 01:52:06] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 19 idle, and 51 total children
[02-Jun-2012 03:10:51] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 18 idle, and 91 total children

我将 php-fpm 的设置更改为这些,

pm.max_children = 150 (It was at 100, i got a max_children reached and upped to 150)
pm.start_servers = 75
pm.min_spare_servers = 20
pm.max_spare_servers = 150

导致

[02-Jun-2012 01:39:19] WARNING: [pool www] server reached pm.max_children setting (150), consider raising it

我刚刚推出了一个新网站,该网站的流量相当可观。这些流量是合法的,当达到限制时,用户会收到 504 网关超时错误。

我使用 IPTABLES 限制了与服务器的连接,并且正在运行 fail2ban 并跟踪 nginx 访问日志。所有流量都是合法的,只是用户空间不足了。

我目前正在一个双核机器上运行 64 位 Ubuntu。

free
             total       used       free     shared    buffers     cached
Mem:       6114284    5726984     387300          0     141612    4985384
-/+ buffers/cache:     599988    5514296
Swap:       524284       5804     518480

我的 php.ini max_input_time = 60

我的 nginx 配置是

worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 19000;
    # multi_accept on;
}
worker_rlimit_nofile    20000;  #each connection needs a filehandle (or 2 if you are proxying)

client_max_body_size 30M;
client_body_timeout   10;
client_header_timeout 10;
keepalive_timeout     5 5;
send_timeout          10;

    location ~ \.php$ {
    try_files $uri /er/error.php;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_max_temp_file_size 0;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

我该怎么做才能避免连接耗尽?为什么这种情况一直发生?我正在实时监控 Google Analytics 上的流量,当用户数超过 120 时,我的 php-fpm.log 就会充满这些警告。

答案1

您是否考虑过按照日志消息中提供的良好建议提高 的值pm.max_children?您有大量可用 RAM 来容纳它们。

回答您的问题:

  • 我该怎么做才能解决连接耗尽的问题?提供更多连接,或减少接收的连接数量。
  • 为什么这种情况不断发生?因为你不断耗尽连接。

答案2

我们的网络服务器也遇到了同样的问题。

您可以尝试在每次 X 请求时重新生成子进程,以避免内存泄漏。它在 Apache 和 FPM 中运行良好,并且也开始运行良好。

 pm.max_requests = 50000

每 50k 个请求就会重启一个子进程

相关内容