php-fpm 如何使用动态流程管理器管理工人?

How php-fpm manages workers with dynamic process manager?

我想阐明 php-fpm 如何使用 dynamic 进程管理器管理 worker。

假设我们有以下配置:

pm = dynamic
pm.max_children = 100
pm.start_servers = 30
pm.min_spare_servers = 20
pm.max_spare_servers = 60
  1. 当 php-fpm 启动时,它产生 30 个进程
  2. 没有连接。 php-fpm 会根据 min_spare_servers 设置关闭 10 个 worker 吗?如果是,什么时候会发生?
  3. 有 40 个到 nginx 的连接。 php-fpm 会为每个连接提供单独的 worker,并立即产生额外的 worker 来满足剩余的连接吗?
  4. 有 80 个到 nginx 的连接。 php-fpm 在启动 60 个 worker 后会如何表现?与(3)相同?
  5. 有 120 个到 nginx 的连接。将 100 个工作人员分配给 100 个连接后会发生什么? php-fpm 是否为连接使用了一些队列?会限制nginx吗? php-fpm 会开始断开连接并显示消息“server reached pm.max_children setting”吗?
    1. 有 50 个到 nginx 的连接。 nginx 会从 100 回到 60 连接吗?还是到50?它会立即杀死 40 名工人还是会等待一段时间?

如您所见,这是一个关于 php-fpm 如何管理进程的普遍问题。更具体地说,我想了解 pm.max_childrenpm.max_spare_servers 在 php-fpm 中的区别.

首先,让我们假设我们谈论 connection/request 到上游的不是 nginx 连接,而是 php-fpm 服务。

  1. there are no connections. Will the php-fpm shut down 10 workers according to min_spare_servers setting? If yes, after what time will it happen?

没有,根据我的测试,master进程不会根据min_spare_servers个数终止extra workers。可能,指定 start_servers 等于 min_spare_servers.

是个好习惯
  1. There are 40 connections to nginx. Will php-fpm serve each connection with separate worker, and immediately spawn additional workers to satisfy remaining connections?

正确,与 php-fpm 的同时连接将由单独的工作程序提供。如果请求数量超过 start_servers 主进程将派生额外的工作进程(fpm_children_make 调用),最多 max_spare_servers.

  1. There are 80 connections to nginx. How will php-fpm behave after it has launched 60 workers? The same as in (3)?

它会分叉尽可能多的工人来同时处理所有请求,直到达到 max_children 数量; fpm master 进程每秒执行一次维护(fpm_pctl_perform_idle_server_maintenance 调用):如果 spawned worker 的数量超过 max_spare_servers,处于空闲状态的 worker 将向 master 进程发送 SIGCHLD 信号(fpm_got_signalfpm_children_bury 个调用)。

  1. There are 120 connections to nginx. What happens after assigning 100 workers to 100 connections? Does php-fpm use some queue for the connections? Will it limit nginx? Will php-fpm start dropping connections with message "server reached pm.max_children setting"?

正确,您将在调试模式下关注消息: seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers

  1. There are 50 connections to nginx. Will nginx go back to 60 connections from 100? Or to 50? Will it immediately kill 40 workers or will it wait some time?

所有处于空闲状态的worker将被终止,master进程将在达到max_spare_servers个数后停止终止。
参数min_spare_serversmax_spare_servers负责idle状态下同时存活的worker的最小和最大数量。


为了理解 dipper,请尝试在 php-fpm.conf 中打开调试日志记录: ... error_log = /var/log/php5-fpm/fpm-daemon.log ... log_level = debug ... 按照日志文件:tail -f /var/log/php5-fpm/fpm-daemon.log 并使用 Apache 基准测试工具 ab 了解行为。