You can replace the old one with the new nginx executable without interrupting service - new requests will not be lost (when upgrading to a new version or adding /when removing the server module). (Recommended learning: nginx operation and maintenance)
First, use the new executable program to replace the old one (it is best to make a backup), and then, send USR2 ( kill -USR2 pid) signal to the main process.
The main process will rename its .pid file to .oldbin (for example: /usr/local/nginx/logs/nginx.pid.oldbin), and then execute the new executable program, in sequence Start a new main process and a new worker process:
PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 33135 33126 nobody 0.0 1380 kqread nginx: worker process (nginx) 33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
At this time, two nginx instances will run at the same time and process incoming requests together. To phase out the old instance, you must send the WINCH signal to the old master process, and then its worker processes will begin to gracefully shut down:
PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33135 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
After some time, the old worker process has processed all connected After exiting the request, only the new worker process will process the incoming request:
PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
At this time, because the old server has not yet closed the socket it is listening to, so, through the following You can still restore the old server with a few steps:
Send HUP signal to the old master process - it will start its worker processes without reloading the configuration file
Send QUIT signal to the new main process, requiring it to shut down its work process gracefully
Send TERM signal to the new main process, forcing it to exit
If for some reason the new job The process cannot exit and the KILL signal is sent to it
After the new main process exits, the old main process will remove the .oldbin prefix and restore it to its .pid file. In this way, everything will be restored to the upgrade Before.
If the upgrade attempt is successful and you also want to keep the new server, send a QUIT signal to the old main process to exit and leave only the new server running:
PID PPID USER %CPU VSZ WCHAN COMMAND 36264 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx)
The above is the detailed content of How to upgrade smoothly in nginx operation and maintenance. For more information, please follow other related articles on the PHP Chinese website!