php: Why does php-fpm smooth restart not work?

1. Problem

        Today I modified some configurations of fpm. I need to go online and restart fpm. However, I found that there were many 502 error requests in an instant. I checked the log and found the following error.

fpm: restart log

 nginx: error log

2023/04/23 15:19:00 [error] 9#0: *1893053661 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: *****, server: ***.com

2023/04/23 15:19:00 [error] 9#0: *1893053661 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: *****, server: ***.com

2. Reasons

        Through investigation, it was finally found that during the reloading of the configuration file, the PHP-FPM master process will close all existing sub-processes, re-create new sub-processes and load the new configuration file. During this process, if a client makes a request, the request will fail and a 502 error will be returned.

  Test Case:

<?php
  echo "测试开始";
  sleep(10);
  echo "测试结束";

 Visit, and then execute kill -USR2 maserid during the visit, and 502 will appear on the page.

3. Solution

  1. Gradually smooth restart: kill -USR2When using the command, you can use php-fpm -tthe command to test whether the new configuration is correct, and then use kill -WINCHthe command to gradually restart PHP-FPM smoothly to avoid service interruption caused by shutting down all child processes in an instant.

  2. Configure process smooth restart: In the PHP-FPM configuration file, you can set process_control_timeoutparameters to control the maximum time for each child process to exit. When reloading the configuration file, the master process will wait for all child processes to shut down gracefully within this time to avoid service interruption. (This method is also mentioned in a bug reported to the official. The suggestion is [2013-02-13 15:57 UTC] phpbugs at oops dot mooo dot com Try setting process_control_timeout to something higher than 0. But rememberprocess_control_timeout配置并不是所有php版本都支持,使用前一定要测试自己的php版本是否支持该配置,目前一般都是7.2以上才支持 )

    process_control_timeout = 20s
    
    ;process_control_timeout 设置子进程接受主进程复用信号的超时时间。可用单位:s(秒)、m(分)、h(小时)或者 d(天)。默认单位:s(秒)。参数缺省是 0。
    即reload的时候,如果有正在执行的请求进程便会等待该进程设置的时长。而其他进程直接就结束掉。等待正在执行的进程执行完或者是超过了设置的时间后fpm的master进程才开始生成新的fpm worker进程。
  3. Use process management tools: Use process management tools, such as Supervisor, Systemd, Upstart, etc., to start and manage the PHP-FPM process, which can better control the life cycle of the process and restart smoothly, thereby reducing the risk of service interruption.

       

In short, when reloading the PHP-FPM configuration file, you need to consider the possibility of service interruption, take appropriate measures to reduce the risk, and ensure the availability of the service as much as possible.

おすすめ

転載: blog.csdn.net/panjiapengfly/article/details/130325840