Nginx Start, Restart, and Stop Commands

Stopping Nginx
Stopping Nginx is done by sending signals to the nginx process (refer to Linux articles for what signals are).
Step 1: Find the Nginx master process ID
ps -ef | grep nginx
In the process list, look for the master process; its number is the master process ID.
Step 2: Send the signal
Graceful shutdown of Nginx:
kill -QUIT master_process_id
Fast shutdown of Nginx:
kill -TERM master_process_id
Force shutdown of Nginx:
pkill -9 nginx

Additionally, if a pid file path is configured in nginx.conf, that file contains the Nginx master process ID; if not specified, it is placed in the nginx logs directory. With the pid file, we don’t need to query the Nginx master process ID first and can send signals directly to Nginx using the command:
kill -signal_type '/usr/nginx/logs/nginx.pid'

Graceful Restart
If you change the configuration, you need to restart Nginx. Should you close Nginx first and then open it? No, you can send a signal to Nginx for a graceful restart.
Graceful restart command:
kill -HUP master_process_id_or_pid_file_path

Or use

/usr/nginx/sbin/nginx -s reload

 

 

Note: After modifying the configuration file, it’s best to first check if the modified configuration file is correct to avoid errors after restarting Nginx that could affect the server’s stable operation. The command to check if the Nginx configuration is correct is as follows:
nginx -t -c /usr/nginx/conf/nginx.conf

Or

/usr/nginx/sbin/nginx -t

 


Graceful Upgrade
If the Nginx server currently running needs an upgrade, or to add or remove modules, we need to stop the server and make the corresponding modifications, which means the server will be out of service for a period of time. Nginx can perform various upgrade actions without downtime, not affecting server operation.
Step 1:
If upgrading the Nginx program, first replace the old program files with the new ones. If compiling and installing, compile the new program directly into the Nginx installation directory.
Step 2: Execute the command
kill -USR2 old_master_process_id_or_process_file_name
At this point, the old Nginx master process will rename its process file to .oldbin and then execute the new version of Nginx. The old and new Nginx will run concurrently, handling requests together.
To gradually stop the old version of Nginx, enter the command:
kill -WINCH old_master_process_id
Slowly, the old worker processes will exit as tasks are completed, and the new Nginx worker processes will gradually replace the old ones.

At this point, we can decide whether to use the new version or revert to the old version.
Start new/old worker processes without reloading configuration
kill -HUP old/new_master_process_id
Gracefully shut down old/new processes
kill -QUIT old/new_master_process_id
If an error occurs at this point, indicating there are still processes that haven’t ended, use the following command to first close the old/new worker processes, then close the master process ID:
kill -TERM old/new_worker_process_id


In this way, if you want to revert to the old version, just operate on the new master process ID in the steps above; if you want to use the new version, operate on the old master process ID in the steps above.

The above are some basic operations for Nginx. Hopefully, in the future, Nginx will have better methods to handle these operations, preferably Nginx commands rather than sending system signals to the Nginx process.

Leave a Comment

Your email address will not be published.