17 Nginx服务的信号控制之USR2

17 Nginx服务的信号控制之USR2

17.1 USR2

  发送USR2信号给master进程,通知master进程要平滑升级,这时,服务器会开启新的master进程和worker进程,整个系统中将会有两个master进程,并且新的master进程的PID会被记录在 /usr/local/nginx/logs/nginx.pid 文件中,而之前旧的master进程PID会被记录在 /usr/local/nginx/logs/nginx.pid.oldbin 文件中,接着再发送QUIT信号给旧的master进程,让其处理完现有请求后再关闭,从而实现平滑升级

架构图:

image

 

# 演示USR2
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx 
[root@nginx-100 ~]# ps -ef|grep nginx
root       1496      1  0 11:15 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1497   1496  0 11:15 ?        00:00:00 nginx: worker process
root       1499   1451  0 11:15 pts/0    00:00:00 grep --color=auto nginx
# 平滑升级
[root@nginx-100 ~]# kill -USR2 1496
[root@nginx-100 ~]# ps -ef|grep nginx
root       1496      1  0 11:15 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1497   1496  0 11:15 ?        00:00:00 nginx: worker process
root       1552   1496  0 12:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1553   1552  0 12:19 ?        00:00:00 nginx: worker process
root       1555   1451  0 12:19 pts/0    00:00:00 grep --color=auto nginx
# 旧master进程 nginx.pid.oldbin、新master进程nginx.pid
[root@nginx-100 ~]# ll /usr/local/nginx/logs/
total 12
-rw-r--r-- 1 nobody root  0 Mar 16 23:10 access.log
-rw-r--r-- 1 nobody root 71 Mar 18 12:19 error.log
-rw-r--r-- 1 root   root  5 Mar 18 12:19 nginx.pid
-rw-r--r-- 1 root   root  5 Mar 18 11:15 nginx.pid.oldbin
# QUIT "优雅"退出
[root@nginx-100 ~]# kill -QUIT 1496
[root@nginx-100 ~]# ps -ef|grep nginx
root       1552      1  0 12:19 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     1553   1552  0 12:19 ?        00:00:00 nginx: worker process
root       1567   1451  0 12:22 pts/0    00:00:00 grep --color=auto nginx
# 升级好后,仅剩下新master进程nginx.pid
[root@nginx-100 ~]# ll /usr/local/nginx/logs/
total 8
-rw-r--r-- 1 nobody root  0 Mar 16 23:10 access.log
-rw-r--r-- 1 nobody root 71 Mar 18 12:19 error.log
-rw-r--r-- 1 root   root  5 Mar 18 12:19 nginx.pid
# error 日志中会有一条记录
[root@nginx-100 ~]# cat /usr/local/nginx/logs/error.log
2026/03/18 12:19:19 [notice] 1552#0: using inherited sockets from "6;"

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-03-16 22:05  马俊南  阅读(2)  评论(0)    收藏  举报