How to close the worker process gracefully?


When we explained the nginx command line before, we can see that there are two ways to stop nginxnginx -s quitandnginx -s stopWhere stop means to stop nginx immediately, and quit means to turn off nginx gracefully. The corresponding signal is the same. In addition, the processes of reload and hot upgrade mentioned earlier involve graceful stopping nginx.

What is the so-called graceful stop nginx process? Let me learn from it.

What is graceful closure?

The so-called graceful shutdown is for the worker process, because only the worker process can handle the request. If we are processing a connection, no matter what the connection does to the request at this time, directly closing the link will cause the user to receive an error. So closing gracefully means that the nginx worker process can recognize that the current connection is not processing the request, and then close the connection at this time.

For some requests, nginx can’t gracefully shut down the worker process. For example, when nginx proxy the websocket protocol, nginx does not parse the frame behind the websocket. When nginx acts as the reverse proxy of TCP layer or UDP layer, there is no way to identify how many packets a request needs to go through before it ends HTTP request, nginx can do, so graceful closing is mainly for HTTP request.

Next, let’s take a look at the processes that gracefully close the worker process.

Elegant closure process

How to close the worker process gracefully?

First of all, the first step is to set a timer in the nginx.conf You can configure a worker in_ shutdown_ Timeout, configure the worker_ shutdown_ After timeout, a flag will be added to indicate the graceful closing process.

The second step is to close the listener handle to ensure that the worker process will not process new connections.

Next, we will look at the connection pool first. In order to maximize the utilization of resources, nginx often saves some idle connections but does not disconnect them. At this time, it will first close the idle connections.

The fourth step may be a very time-consuming step, because nginx does not automatically shut down immediately. Instead, it uses the flag bit added in the first step. Every time a request is found to have been processed in the loop, the connection used by the request will be shut down. Therefore, it may take a long time to wait for closing in the loop. When worker is set_ shutdown_ When timeout, even if the request is not finished, when the time is up, these requests will be forced to close, that is to say, the graceful closing is only half finished, and some connections will stop immediately.

So there are two conditions: when the connection is gracefully closed in all loops, or the worker is reached_ shutdown_ After the timeout timer, the worker process will exit immediately.


This article mainly explains a process of graceful shutdown of worker process. Many times, we will use the feature of graceful shutdown of nginx, so when this feature fails, we need to consider nginx Whether it has the ability to determine whether a connection should be shut down correctly at this time; or if there is an error, some modules or some clients can’t handle requests normally, nginx needs to have some exceptional measures, such as worker_ shutdown_ Timeout to ensure that the old worker process of nginx can exit normally.