16 Nginx服务的信号控制
16 Nginx服务的信号控制
16.1 提问
1.Nginx中的master进程和worker进程分别是什么?它们之间的关系及各自作用是什么? 2.Nginx的工作方式有哪些? 3.Nginx中如何获取当前正在运行进程的PID? 4.上述中信号有哪些?各自作用是什么? 5.如何通过信号控制Nginx的启停等相关操作?
16.2 架构
前面提到Nginx的高性能,其实也和它的架构模式有关,Nginx 默认采用的是多进程的方式工作,当将Nginx启动后,通过 ps -ef|grep nginx 命令查看
[root@nginx-100 ~]# ps -ef|grep nginx root 4505 1 0 21:03 ? 00:00:00 nginx: master process ./nginx nobody 4506 4505 0 21:03 ? 00:00:00 nginx: worker process root 4602 1357 0 22:34 pts/0 00:00:00 grep --color=auto nginx
Nginx后台进程中包含一个master进程和多个worker进程
master 进程主要用来管理worker进程,包含接收外界的信号,并将接收到的信号发送给各个worker进程,监控worker进程的状态,当worker进程出现异常退出后,会自动重新启动新的worker进程
worker 进程则是专门用来处理用户请求的,各个worker进程之间是平等且相互独立的,处理请求的机会也是相同的
nginx的进程模型:1.管理员、2.master进程、3.worker进程、4.用户

作为管理员,只需要通过给 master 进程发送信号就可以控制Nginx,两种方式:1.操作master进程、2.操作信号
16.3 操作master进程
1.操作nginx的master进程,需要获取到master进程的进程号ID,两种获取方式:
方式一:通过 ps -ef|grep nginx
方式二:在讲解nginx的 ./configure 的配置参数时,有参数 --pid-path=PATH,默认是 /usr/local/nginx/logs/nginx.pid,所以可以通过查看该文件来获取nginx的master进程ID
[root@nginx-100 ~]# ps -ef|grep nginx root 1491 1 0 22:36 ? 00:00:00 nginx: master process ./nginx nobody 1492 1491 0 22:36 ? 00:00:00 nginx: worker process root 1552 1454 0 22:41 pts/0 00:00:00 grep --color=auto nginx [root@nginx-100 ~]# cat /usr/local/nginx/logs/nginx.pid 1491
16.4 操作信号
| 信号 | 作用 |
| TERM / INT | 立即关闭整个服务(即使worker进程在工作,仍旧立即退出) |
| QUIT | "优雅"地关闭整个服务(先将worker进程不再接收新请求,将当前已有的请求处理完成后再将master 进程、worker进程一并退出) |
| HUP | 重读配置文件并使用服务对新配置项生效 |
| USR1 | 重新打开日志文件,可以用来进行日志切割 |
| USR2 | 平滑升级到最新版的nginx |
| WINCH | 所有子进程不在接收处理新连接,相当于给worker进程发送QUIT指令(仅worker进程退出,master 进程不退出) |
使用命令:kill -signal PID
signal:上表中信号
PID:获取到的 master 进程ID
1.发送TERM / INT信号给master进程,会将Nginx服务立即关闭
# 演示 TERM
[root@nginx-100 ~]# ps -ef|grep nginx root 1491 1 0 22:36 ? 00:00:00 nginx: master process ./nginx nobody 1492 1491 0 22:36 ? 00:00:00 nginx: worker process root 1574 1454 0 22:59 pts/0 00:00:00 grep --color=auto nginx [root@nginx-100 ~]# kill -TERM 1491 [root@nginx-100 ~]# ps -ef|grep nginx root 1576 1454 0 23:00 pts/0 00:00:00 grep --color=auto nginx
2.发送QUIT信号给master进程,master进程会控制所有的worker进程不再接收新的请求,等所有请求处理完后,再把进程都关闭掉
# 演示 QUIT [root@nginx-100 ~]# /usr/local/nginx/sbin/nginx [root@nginx-100 ~]# ps -ef|grep nginx root 1593 1 0 23:01 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 1594 1593 0 23:01 ? 00:00:00 nginx: worker process root 1596 1454 0 23:01 pts/0 00:00:00 grep --color=auto nginx [root@nginx-100 ~]# kill -QUIT 1593 [root@nginx-100 ~]# ps -ef|grep nginx root 1598 1454 0 23:02 pts/0 00:00:00 grep --color=auto nginx
3.发送HUP信号给master进程,master进程会控制旧的worker进程不再接收新的请求,等处理完请求后将旧的worker进程关闭掉,然后根据nginx的配置文件重新启动新的worker进程
# 演示 HUP,master 主进程不变,worker 进程重新生效 [root@nginx-100 ~]# /usr/local/nginx/sbin/nginx [root@nginx-100 ~]# ps -ef|grep nginx root 1602 1 0 23:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 1603 1602 0 23:04 ? 00:00:00 nginx: worker process root 1605 1454 0 23:04 pts/0 00:00:00 grep --color=auto nginx [root@nginx-100 ~]# kill -HUP 1602 [root@nginx-100 ~]# ps -ef|grep nginx root 1602 1 0 23:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 1606 1602 0 23:05 ? 00:00:00 nginx: worker process root 1608 1454 0 23:05 pts/0 00:00:00 grep --color=auto nginx
4.发送USR1信号给master进程,告诉Nginx重新开启日志文件
# 演示 USR1,重新生成日志文件,master 、worker 进程号不变 [root@nginx-100 ~]# ps -ef|grep nginx root 1602 1 0 23:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 1606 1602 0 23:05 ? 00:00:00 nginx: worker process root 1611 1454 0 23:07 pts/0 00:00:00 grep --color=auto nginx [root@nginx-100 ~]# ll /usr/local/nginx/logs/ total 12 -rw-r--r-- 1 root root 1352 Mar 15 21:23 access.log -rw-r--r-- 1 root root 693 Mar 15 21:23 error.log -rw-r--r-- 1 root root 5 Mar 16 23:04 nginx.pid [root@nginx-100 ~]# rm -rf /usr/local/nginx/logs/access.log /usr/local/nginx/logs/error.log [root@nginx-100 ~]# kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` [root@nginx-100 ~]# ll /usr/local/nginx/logs/ total 4 -rw-r--r-- 1 nobody root 0 Mar 16 23:10 access.log -rw-r--r-- 1 nobody root 0 Mar 16 23:10 error.log -rw-r--r-- 1 root root 5 Mar 16 23:04 nginx.pid [root@nginx-100 ~]# ps -ef|grep nginx root 1602 1 0 23:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 1606 1602 0 23:05 ? 00:00:00 nginx: worker process root 1656 1454 0 23:11 pts/0 00:00:00 grep --color=auto nginx
5.发送WINCH信号给master进程,master进程会控制所有的worker进程不再接收新的请求,请求处理完后关闭worker进程
注意master进程不会被关闭掉
# 演示 WINCH,master 进程未退出,仅 worker 进程关闭 [root@nginx-100 ~]# ps -ef|grep nginx root 1602 1 0 23:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nobody 1606 1602 0 23:05 ? 00:00:00 nginx: worker process root 1660 1454 0 23:18 pts/0 00:00:00 grep --color=auto nginx [root@nginx-100 ~]# kill -WINCH 1602 [root@nginx-100 ~]# ps -ef|grep nginx root 1602 1 0 23:04 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx root 1662 1454 0 23:19 pts/0 00:00:00 grep --color=auto nginx
———————————————————————————————————————————————————————————————————————————
无敌小马爱学习
浙公网安备 33010602011771号