Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are nginx smooth restart and FPM smooth restart

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is nginx smooth restart and FPM smooth restart", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what is nginx smooth restart and FPM smooth restart"!

graceful restart

GR is short for Graceful Restart, which is a mechanism to ensure that forwarding services are not interrupted during protocol restart.

The core of GR mechanism is that when a device restarts, it can notify its peripheral devices to keep the neighbor relationship and route to the device stable for a certain period of time. After the protocol is restarted, peripheral devices assist it in synchronizing information (including various topology, routing and session information maintained by routing/MPLS protocols supporting GR), so that the device can recover to the state before restart in as short a time as possible. No route oscillation occurs during the whole protocol restart process, and the message forwarding path does not change. The whole system can forward data without interruption. This process is called a smooth restart.

nginx smooth restart

The nginx process is divided into master process and worker process, and the smooth restart of nginx is controlled by signal HUB.

Note: On POSIX-compatible platforms, SIGUSR1 and SIGUSR2 are signals sent to a process that represent user-defined conditions.

In order to analyze the smooth restart process of nginx in detail, we continuously monitor nginx process changes.

Send HUP signal

kill -HUP `cat /home/git/nginx/logs/nginx.pid`

Through observation, we can analyze the approximate smooth restart process as follows:

1. master uses new configuration fork out n-1 worker and new master

2. The new worker handles the new request, and the old worker exits after execution.

3. master reloads the configuration while using the new master to take over the service

4. Master loading configuration completed, new master switch to worker mode

After a smooth restart, the master process number does not change.

nginx smooth upgrade

HUP is only used for smooth restart, loading configuration, etc. If you want to upgrade nginx version smoothly, reload compiled binary files, you need to use USR2 signal.

1. Send USR2 signal

kill -USR2 `cat /home/git/nginx/logs/nginx.pid`

Observed nginx process, fork new master and worker, at this time nginx.pid content has changed, and generated nginx.pid.oldbin file in logs directory, record old master pid.

2. By sending WINCH to the old master, nginx walker gracefully stops serving, i.e., stops receiving new requests, but does not terminate requests already being processed. After a while, all the worker processes of the old nginx exit, leaving only the master process, and all user requests are handled by the new nginx process.

kill -WINCH `cat /home/git/nginx/logs/nginx.pid.oldbin`

3, send QUIT signal to the old master, the old nginx process completely quit, so far the smooth upgrade is complete.

kill -QUIT `cat /home/git/nginx/logs/nginx.pid.oldbin`

FPM smooth restart

FPM (FastCGI Process Manager) is used to replace most of the additional features of PHP FastCGI. FPM has been integrated since php 5.3.3. PHP-FPM can be turned on with the-enable-fpm parameter when configuring.

The smooth restart of FPM needs to be controlled by USR2 signal, but it is quite different from the smooth restart process of nginx.

kill -USR2 `cat /home/git/php/var/run/php-fpm.pid`

By continuing to observe the fpm process, you can see that the FPM restarts smoothly, and it needs to wait until the child process completely exits before starting the new master and child process, and then the old master exits.

Further analysis using strace

The discovery master notifies all child processes to exit, including the child process that is processing the request.

To further verify this conclusion, write a server-side sleep script

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report