Nginx 如何优雅下线服务器 (nginx 下线服务器)
Introduction
Nginx is one of the most popular web servers, and it’s widely used by software developers, system administrators, and businesses worldwide. Nginx allows users to quickly and efficiently serve static and dynamic content, balance traffic between multiple servers, and implement security features to protect agnst potential attacks. However, one less talked about feature of Nginx is how it gracefully handles server shutdowns or reboots.
In this article, we will explore how Nginx handles server shutdowns and reboots and offer some best practices for gracefully shutting down your Nginx server. We will discuss different strategies for handling server shutdowns based on the specific situation.
Why Graceful Shutdown is Important?
Graceful shutdown is important for mntning the avlability of a high-performing web server. Whenever you take down or restart a server, you need to ensure that the downtime is as minimal as possible. This is especially important when you run a web application on your server. An abrupt shutdown will cause the server to stop accepting new requests immediately, potentially causing data loss and other problems. On the other hand, gracefully shutting down the server will give it time to complete any outstanding requests and ensure all data is written to the disk before the shutdown occurs.
How Nginx Handles Server Shutdowns?
When you need to take down an Nginx server, it’s essential to understand how the server handles server shutdowns. Nginx listens to the master process for signals such as SIGTERM or SIGQUIT that are sent during a shutdown. When the master process receives these signals, it starts a process of gracefully stopping all child worker processes.
Once all of the worker processes complete their current requests and become idle, they begin to close one by one. The master process will then terminate when all of the child processes have stopped completely. By default, the shutdown period for Nginx is set to ten seconds.
Nginx gracefully handles shutdowns by closing all connections before shutting down. This means that clients will not receive half-open connections, leading to retranissions and wasted bandwidth. Nginx will also write any data that needs to be persisted to the disk, ensuring there is no data loss due to a server crash.
How to Gracefully Shutdown Nginx?
Here are some best practices for gracefully shutting down Nginx:
1. Using Systemd
One of the most strghtforward approaches to gracefully shutting down Nginx is using the systemd init system. Systemd was introduced in CentOS 7, Fedora 15, and Arch Linux and acts as a central management system for running and controlling services.
To gracefully shut down Nginx using systemd, you run the following command:
sudo systemctl stop nginx
This command sends the SIGTERM signal to the master process and starts the graceful shutdown process for all worker processes.
2. Using Nginx Control Script
If your operating system doesn’t use systemd, you can use the Nginx control script to gracefully shut down Nginx. The script is typically found in the /etc/init.d/ directory, and it controls Nginx as a service.
To gracefully shut down Nginx using the control script, you run the following command:
sudo /etc/init.d/nginx stop
This command sends the SIGTERM signal to the master process and starts the graceful shutdown process for all worker processes.
3. Sending a Manual Signal
If you need more control over the shutdown process, you can send a manual signal to the master process. This approach is useful if you need to control how long a client connection remns open before the server closes it.
To gracefully shut down Nginx manually, you run the following command to find the PID of the Nginx master process:
ps -aux | grep nginx
Once you have the PID, you can send a SIGQUIT or SIGTERM signal to the master process using the kill command:
sudo kill -QUIT # Sends the SIGQUIT signal
or
sudo kill -TERM # Sends the SIGTERM signal
Conclusion