PHP-FPM ondemand:活跃子节点数量达到 max_children 时,php 停止工作

PHP-FPM ondemand:活跃子节点数量达到 max_children 时,php 停止工作

我的服务器运行10个网站,流量很低。配置:

  • Ubuntu 20.04.5 LTS
  • Nginx 1.18.0(Ubuntu)
  • PHP 7.4.3

nginx.conf这是添加的:

        upstream local_php {
                server unix:/run/php/php7.4-fpm.sock;
        }

已启用站点配置文件的位置包括:

        location ~ \.php$ {
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass local_php;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
        }

我的/etc/php/7.4/fpm/pool.d/www.conf有:

pm = ondemand
pm.max_children = 15
pm.max_requests = 10

现在的情况是,我的网站运行良好,但 php 日志文件显示活跃子进程的数量在稳步增加。大约一天后,子进程数量达到 15,然后 PHP 停止工作。查看进程列表,所有“年龄”的子进程似乎都毫无目的地存在着。PHP 日志文件中没有警告,只有当max_children达到以下值时才会显示警告:

WARNING: pid 75057, fpm_pctl_on_socket_accept(), line 518: [pool www] server reached max_children setting (15), consider raising it

我一直在尝试调整 PHP 设置。使用pm = dynamic,可以更快地达到 max_children 。max_requests起初较高,但降低它并没有显着差异。我有足够的资源来增加max_children一点,但这只是推迟了问题,而不是解决问题。

请注意,我的服务器没有负载问题。内存使用率从未超过 35%,CPU 稳定在 5%。

我肯定做错了什么,因为我认为孩子们最终应该被杀死,不是吗?PHP 日志表明子进程正在被杀死,但是这总是会减少备用子进程的数量,甚至减少活跃子进程的数量:

DEBUG: pid 232350, fpm_pctl_perform_idle_server_maintenance(), line 365: [pool www] currently 8 active children, 2 spare children
DEBUG: pid 232350, fpm_got_signal(), line 82: received SIGCHLD
DEBUG: pid 232350, fpm_event_loop(), line 435: event module triggered 1 events
DEBUG: pid 232350, fpm_children_bury(), line 261: [pool www] child 289966 has been killed by the process management after 12.069386 seconds from start
DEBUG: pid 232350, fpm_pctl_perform_idle_server_maintenance(), line 365: [pool www] currently 8 active children, 1 spare children
DEBUG: pid 232350, fpm_got_signal(), line 82: received SIGCHLD
DEBUG: pid 232350, fpm_event_loop(), line 435: event module triggered 1 events
DEBUG: pid 232350, fpm_children_bury(), line 261: [pool www] child 289969 has been killed by the process management after 12.665847 seconds from start
DEBUG: pid 232350, fpm_pctl_perform_idle_server_maintenance(), line 365: [pool www] currently 8 active children, 0 spare children

典型的流程列表,子进程最多可达 15 个:

1390 vps@vps9029:/etc/php/7.4/fpm/pool.d $ ps -elf|grep php
4 S root    15528   15439  0  80   0 -  2397 -      Dec20 pts/1 00:00:00 sudo tail -f php7.4-fpm.log
4 S root    15537   15528  0  80   0 -  1378 -      Dec20 pts/1 00:00:12 tail -f php7.4-fpm.log
4 S root    75057       1  0  80   0 - 59077 -      Dec21 ?     00:00:26 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
5 S www-data   94817   75057  0  80   0 - 84357 -   Dec21 ?     00:00:00 php-fpm: pool www
5 S www-data  104885   75057  0  80   0 - 84302 -   Dec21 ?     00:00:00 php-fpm: pool www
5 S www-data  125566   75057  0  80   0 - 66282 -   Dec21 ?     00:00:01 php-fpm: pool www
5 S www-data  143879   75057  0  80   0 - 65617 -   02:47 ?     00:00:00 php-fpm: pool www
5 S www-data  149198   75057  0  80   0 - 84441 -   03:56 ?     00:00:00 php-fpm: pool www
5 S www-data  149632   75057  0  80   0 - 84582 -   04:02 ?     00:00:08 php-fpm: pool www
5 S www-data  152959   75057  0  80   0 - 84515 -   04:43 ?     00:00:01 php-fpm: pool www
5 S www-data  178687   75057  0  80   0 - 65673 -   09:53 ?     00:00:00 php-fpm: pool www
5 S www-data  182987   75057  0  80   0 - 84178 -   10:46 ?     00:00:00 php-fpm: pool www
5 S www-data  187712   75057  0  80   0 - 84178 -   11:44 ?     00:00:00 php-fpm: pool www
5 S www-data  187713   75057  0  80   0 - 84178 -   11:44 ?     00:00:00 php-fpm: pool www
5 S www-data  197529   75057  0  80   0 - 84386 -   13:59 ?     00:00:00 php-fpm: pool www
5 S www-data  210404   75057  0  80   0 - 65569 -   16:48 ?     00:00:00 php-fpm: pool www
5 S www-data  213858   75057  0  80   0 - 65633 -   17:29 ?     00:00:00 php-fpm: pool www
5 S www-data  214975   75057  0  80   0 - 85465 -   17:44 ?     00:00:00 php-fpm: pool www

答案1

原因是其中一个已安装的站点的请求很慢。

有很多 FPM/NGINX 设置可以控制子进程的运行。这些是我现在的设置,它们似乎可以控制子进程:

/etc/php/7.4/fpm/pool.d/www.conf

pm = ondemand
pm.max_requests = 10
pm.max_children = 15
pm.process_idle_timeout = 10s
request_terminate_timeout = 60s
  • request_terminate_timeout_track_finished = yes

/etc/php/7.4/fpm/php.ini

max_execution_time = 30
default_socket_timeout = 60

/etc/nginx/nginx.conf

keepalive_timeout 55;
fastcgi_read_timeout 60;

相关内容