Nginx is a multi-process program, communication between multiple processes can use shared memory, signals, etc. When it comes to interprocess management, only signals are usually used.

Today we’ll take a look at how signals are used in Nginx process management.

Nginx process Management: Signals

As can be seen from the figure above, the master process, worker process and Nginx command line can send and process signals.

First let’s look at the Master process.

The Master process

Since the master process will start the worker process, the first way it manages the worker process is to monitor whether the worker process sends THE CHLD signal, because it is stipulated in the Linux operating system that the child process will send the CHLD signal to the parent process when it terminates. Therefore, if the worker process terminates unexpectedly due to some module code bug, the master process can immediately find such an event through CHLD and pull the worker process again.

The Master process also manages the worker process by receiving some signals.

The Master process can accept the following signals:

  • TERM, INT: Stop the Nginx process immediately
  • QUIT: Gracefully stop the Nginx process without immediately sending the user a connection termination request (such as a TCP reset request).
  • HUP: reloads the configuration file
  • USR1: reopens the log file and cuts the log file
  • USR2: used for hot deployment
  • WINCH: gracefully exit all worker processes

USR2 and WINCH can only send signals through the kill command line of Linux. That is to say, we need to find the PID of the master process and send USR2 or WINCH to this PID. The other four have corresponding Nginx commands.

Worker processes

The worker process is usually not signaled directly because we want the master process to manage the worker process. Although directly sending signals to worker processes will also make worker processes produce the same results, it is usually not done in this way. It is usually managed by the master process. After receiving signals, the master process will send signals to worker processes.

Nginx command line

After Nginx starts, Nginx puts its PID into a file. By default, the Nginx master pid is recorded in the /logs/nginx.pid file of the Nginx installation directory.

When using the nginx -s command line again, the nginx tool command line will read the PID of the master process in the PID file and send the same signals to the PID such as HUP, USR1, TERM, QUIT. Such a command corresponds to reload, reopen, stop, and quit, so the effect of calling the nginx command line is the same as that of sending signals directly with kill.

conclusion

This article mainly introduces the use of signals in Nginx process management, mainly involving the Master process, Worker process and Nginx command line. We may see on the Internet has a lot of heat management log file or upgrade script, the script is to use the kill signal directly, sometimes is sometimes called nginx – s such a command line, a lot of people may be confused before, so I believe that we should now be very understand how such a process.