Nginx Start, Restart, and Stop Commands

 

Stopping Nginx
Stopping is performed by sending signals to the nginx process (refer to Linux documentation 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
Quick shutdown of Nginx:
kill -TERM master_process_id
Force stop Nginx:
pkill -9 nginx

Additionally, if the pid file path is configured in nginx.conf, that file stores 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 first look up the Nginx master process ID and can send signals directly to Nginx, with commands like:
kill -signal_type '/usr/nginx/logs/nginx.pid'

Graceful Restart
If you change the configuration, you need to restart Nginx. Should you shut down Nginx first and then start 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 is correct to avoid errors after restart that could affect server stability. 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

 


Smooth Upgrade
When the server’s running Nginx needs to be upgraded, or modules added/removed, we need to stop the server and make the corresponding modifications. This means the server will be out of service for some time. Nginx allows various upgrade actions without stopping the server, not affecting server operation.
Step 1:
If upgrading the Nginx program, first replace the old program file with the new one. If compiled and installed, compile the new program directly into the Nginx installation directory.
Step 2: Execute the command
kill -USR2 master_process_id_or_pid_file_of_old_version
At this point, the old Nginx master process will rename its process file to .oldbin, then execute the new version of Nginx. Old and new Nginx will run simultaneously, handling requests together.
Now, gradually stop the old Nginx, enter the command:
kill -WINCH old_master_process_id
Slowly, the old worker processes will exit as tasks complete, and the new Nginx’s worker processes will gradually replace the old ones.

At this point, we can decide to use the new version or revert to the old one.
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 time, indicating that processes are still not finished, use the following command to first close the old/new worker processes, then close the master process:
kill -TERM old/new_worker_process_id


So, to revert to the old version, just operate on the new master process ID in the steps above; to use the new version, operate on the old master process ID in the steps above.

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

Leave a Comment

Your email address will not be published.