Transmission control signal nginx

table of Contents

  • Change setting
  • Scroll log file
  • Upgrade executable file on the fly

It can be controlled by sending a signal to nginx. PID primary process nginx default is written /usr/local/nginx/logs/nginx.pid.
pid file path can be modified at configuration time, using the pid instruction specifies a different path. nginx main process supports the following signals:

 

TERM, INT   快速关闭 nginx
QUIT        优雅地关闭 nginx
HUP         修改配置,keeping up with a changed time zone (only for FreeBSD and Linux),
            以新配置启动新的 worker 进程,优雅地关闭老的 worker 进程。
USR1        重新打开日志文件
USR2        升级可执行文件
WINCH       优雅地关闭 worker 进程

You can also send a signal to a worker process, supported signal are:

 

TERM, INT   快速关闭 nginx
QUIT        优雅地关闭 nginx
USR1        重新打开日志文件
WINCH       为调试bug,非正常地关闭 worker 进程(需在配置中使用 debug_points 指令)

Change setting


Let nginx reload configuration files, you need to send a HUP signal to the main nginx process. After nginx main process receives a signal, first check the syntax of the configuration, and then try to apply the new configuration, such as: open the log file, or create a new listening socket. If you apply the new configuration fails, nginx to roll back the changes, and continue to work with the old configuration. If successful, nginx start a new worker process and send a message close to the old worker process requires them to gracefully shut down. The old worker process shutdown listening socket, and continue through the current user requests, such as the user's request is complete, it will shut down.

We take an example to illustrate this process, the implementation in Linux:
ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'

This command generates the following output:

 

  PID  PPID USER     %CPU    VSZ WCHAN  COMMAND
 5638     1 root      0.0  45116 rt_sig nginx: master process nginx
 5960  5638 nginx     0.0  45356 ep_pol nginx: worker process

When sending a HUP signal to the main process nginx
kill -s HUP 6538

Ps command, you can see PID worker process is changed, the output is changed to:

 

 PID  PPID USER     %CPU    VSZ WCHAN  COMMAND
5638     1 root      0.0  45116 rt_sig nginx: master process nginx
6302  5638 nginx     0.0  45564 ep_pol nginx: worker process

The implementation kill -s HUP 6538effect of the command to reload nginx configuration is the same:
nginx -s reload

Scroll log


Before rolling logs, logs need to be renamed, and then send the USR1 signal to the main nginx process. Master process re-open all current log file has been opened, and the current user identity worker process, to re-open the log file that defines the identity of owner (chown). After a successful re-open, the main process closes any open log file, and sends a message to the worker process requires that it re-open the log file. worker process will open new log file, and close the old log files immediately. After that, the old log files allows for push processing,
such as compression processing.

Example:

cd to the log directory:

 

[root@lamp1 nginx]# cd /var/log/nginx
[root@lamp1 nginx]# ls
access.log  error.log

Rename the file access.log access.log.1, nginx -s reopen execute commands, scroll to view the log situation:

 

[root@lamp1 nginx]# mv access.log access.log.1
[root@lamp1 nginx]# nginx -s reopen
[root@lamp1 nginx]# ls
access.log  access.log.1  error.log

Rename the file error.log is error.log.1, nginx -s reopen execute commands, scroll to view the log situation:

 

[root@lamp1 nginx]# mv error.log error.log.1
[root@lamp1 nginx]# nginx -s reopen
[root@lamp1 nginx]# ls
access.log  access.log.1  error.log  error.log.1

Note: kill -s HUPthe nginx -s reopensame effect.

View the log file's owner, as nginx, nginx is the identity of the worker process execution, look at the front "Edit Configuration" section of the ps command output:

 

[root@lamp1 nginx]# ll
total 12
-rw-r--r-- 1 nginx root    0 Jun 29 07:58 access.log
-rw-r--r-- 1 nginx root 4055 Jun 28 17:29 access.log.1
-rw-r--r-- 1 nginx root   63 Jun 29 07:59 error.log
-rw-r--r-- 1 nginx root 1260 Jun 29 07:58 error.log.1

Upgrade executable file on the fly


Upgrade nginx executable program, first replace the old nginx executable file for the new executable file, and then send a signal to the nginx USR2 primary process. nginx first step in the main process renames pid file, and add to the original pid file .oldbinsuffix, such as /usr/local/nginx/logs/nginx.pid.oldbin, and then start a new nginx executable file and turn starts a new worker process.

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33135 33126 nobody   0.0  1380 kqread nginx: worker process (nginx)
33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

At this time both the old and new worker process to continue to accept requests. If you send a signal to the first main WIHCH process, it sends a message to its worker process, asking them to gracefully shut down after worker process first began to withdraw from the primary process:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33135 33126 nobody   0.0  1380 kqread nginx: worker process is shutting down (nginx)
36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

After a moment, only new worker process to continue processing the request:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

It should be noted that the old master process does not close its listening socket, and can restart its worker process when needed. If for some reason the new executable file does not work correctly, can be one of the following methods:

  • Send HUP signal to the old master process. The old master process starts a new worker process, but it will not re-read the configuration file. After that, the QUIT signal can be sent to the new primary process, so that all new primary process worker process gracefully shut down.

  • TERM signal is sent to the new primary process. After new master process receives a TERM signal, it will send a message to the worker process, asking them to withdraw immediately, after the worker process will exit immediately. (If the worker process for some reason can not quit immediately, should make it mandatory to send KILL signal to quit) after the new master process exits, the old master process starts a new worker process automatically.

If the new primary process exits, .oldbin suffix former team process pid file will be deleted.

If the upgrade is completed successfully, shut down former team should send QUIT signal process, leaving only the new primary process:

 

 PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
36264     1 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

 

Released 2387 original articles · won praise 38 · Views 250,000 +

Guess you like

Origin blog.csdn.net/lxw1844912514/article/details/104599632