Nginx / PHP FPM graceful stop (SIGQUIT): not so graceful

Asked
Active3 hr before
Viewed126 times

5 Answers

nginx
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Stack Overflow en español, Meta Stack Overflow ,Stack Overflow em Português

Unfortunately, it seems that the php-fpm master process exits immediately, so, inspired by the code here, I wrote a wrapper script:

#!/bin/bash

PHP_FPM_PID = '/php-fpm.pid'

wait_for_pid() {
   try = 0

   while test $try - lt 35;
   do
      if [!-f "$1"];
   then
   try = ''
   break
   fi

   echo - n.
   try = `expr $try + 1`
   sleep 1
   done
}

function clean_up {

   echo "Killing $(cat $PHP_FPM_PID)"

   kill - QUIT `cat $PHP_FPM_PID`
   wait_for_pid $PHP_FPM_PID

   echo "Done!"

   exit 0
}

trap clean_up EXIT

nohup php - fpm--daemonize--pid $PHP_FPM_PID 2 > & 1 &

   while true;
do sleep 1;
done
# ^ do nothing forever
88%

Pod Lifecycle – Termination of Pods,The problem is that during downscaling clients get 502 errors. E.g. when a pod is stopping, its containers can not correctly close existing connections.,Pods became Terminating:,See the HTTP persistent connection.

Prepare a Dockerfile:

FROM nginx

RUN echo 'server {\n\
    listen 80 default_server;\n\
    location / {\n\
      proxy_pass      http://httpbin.org/delay/10;\n\
    }\n\
}' > /etc/nginx / conf.d /
   default.conf

CMD["nginx", "-g", "daemon off;"]
load more v
72%

stay  Graceful shutdown in Kubernetes is not always trivial  and Stackoveflow Upper  Nginx / PHP FPM graceful stop (SIGQUIT): not so graceful  I mentioned ,FPM’s master Killing a process before its child also results in 502 error .,Nginx and PHP-FPM hold SIGTERM Signal as a forced end signal , And it will end the process immediately , Instead of processing the current connection, close it immediately ( Reference resources  Controlling nginx  and  php-fpm(8) - Linux man page), our PHP-FPM There is no such problem , Because its original image has been added  STOPSIGNAL SIGQUIT., Use this image and in supervisor.conf In the document Nginx and PHP-FPM To configure stopsignal sigquit, Although the idea seems to be right , It turns out that it doesn't work either .

FROM nginx
RUN echo 'server {\n\
listen 80 default_server;\n\
location / {\n\
proxy_pass http://httpbin.org/delay/10;\n\
}\n\
}' > /etc/nginx / conf.d /
   default.conf
CMD["nginx", "-g", "daemon off;"]
load more v
65%

exist Graceful shutdown in Kubernetes is not always trivialAnd StackoveFlowNginx / PHP FPM graceful stop (SIGQUIT): not so gracefulIn mentioned, the FPM's Master process will cause a 502 error to be killed first.,Nginx and PHP-FPM use the Sigterm signal as a forced end signal, and will immediately end the process, no longer process the current connection but immediately close (referenceControlling nginx and php-fpm(8) - Linux man page),Use this mirror and configure Stopsignal Sigquit to Nginx and PHP-FPM in the Supervisor.conf file, although the idea looks right, the result is not.,In this blog post, let us take a deep look at the end process of the POD, especially Nginx and PHP-FPM containers.

FROM nginx

RUN echo 'server {\n\
    listen 80 default_server;\n\
    location / {\n\
      proxy_pass      http://httpbin.org/delay/10;\n\
    }\n\
}' > /etc/nginx / conf.d /
   default.conf

CMD["nginx", "-g", "daemon off;"]
load more v
75%

Graceful shutdown of programs: this command stop all workers sending SIGSTOP signal, using Supervisord built-in command. Feel free to stop on your own (e.g.: docker exec <CONTAINER_ID> stop) or using Docker one (e.g.: docker stop <CONTAINER_ID>). The entrypoint will trap both SIGTERM and SIGQUIT in order to achieve graceful shutdown, useful if you cannot handle pre-stop scripts provided by some schedulers. Remember that the default amount of seconds before Supervisord will sending SIGCHLD is 10.,MAX_REQUESTS The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries. For endless request processing specify 0,Programs output like error and access log is set redirected to STDOUT, useful in order to use Docker logging capabilities.,Checking every 5 seconds if PHP-FPM and NGINX are running: feel free to provide a /ping endpoint in your application.

docker-php7-nginx

Base image optimised for PHP 7.1 and NGINX, wrapped as Supervisor programs in order to provide a 12 factor app environment. It doesn't bind SSL port (443): load-balancer is responsible for that. Port 8080 in order to don't run as root (<= 1024 ports are privileged).

443
load more v