nginx 进程结构:
为什么nginx采用的是多进程?
nginx 采用的是多进程 process ,而不是多线程,
因为如果是多线程,它们是共用资源的,这样一旦挂了,就都挂了,
进程结构图:
master process 并不真正的处理用户请求,而是下面的worker process 来处理用户的请求,
master 是用来管理的作用:
1,如果woker process 中有进程 宕掉,这是master process 会重新再开一个 进程
2,如果有配置文件修改了,master process 也会通知worker process 去用新的配置文件(热加载)
Linux 的信号量管理机制:
所有的信号:
平时使用的kill + pid,其实是kill -15 pid ,
kill -9 pid 是强制杀死进程,
常用的信号量:
1,sigchld: kill -17 pid ,此时会发送给该进程的父进程,
2,sigquit 退出 (ctrl \ )
3,sigterm 关闭进程
4,sigkill 立即关闭进程
5,sighup 重读配置文件
利用信号量管理nginx:
可以通过信号量管理的进程:
1,Master 进程
2,Worker 进程(不推荐直接给Worker 发送信号 对它进行管理)
Master 进程 接收的信号:
a,监控worker 进程,(如果 worker 进程宕掉,会向master 发送chld 信号 )
b,管理woker 进程,通过 向woker 进程发送 一下信号:
worker 进程 接收的信号:
注:不建议直接给 worker 进程发送信号,而是通过给master 发送信号 来管理worker 进程,
发送 sigterm 给master:
[root@localhost nginx112]# ps -ef | grep nginx root 1568 1 0 16:56 ? 00:00:00 nginx: master process sbin/nginx nobody 1590 1568 0 16:56 ? 00:00:00 nginx: worker process nobody 1591 1568 0 16:56 ? 00:00:00 nginx: worker process nobody 1592 1568 0 16:56 ? 00:00:00 nginx: worker process nobody 1593 1568 0 16:56 ? 00:00:00 nginx: worker process root 1646 1460 0 17:01 pts/0 00:00:00 grep --color=auto nginx [root@localhost nginx112]# kill -s sigterm 1568
发送 sighup 给master:
它会关闭所有worker 进程,重新打开新的进程,
[root@localhost nginx112]# ps -ef | grep nginx root 1650 1 0 17:03 ? 00:00:00 nginx: master process sbin/nginx nobody 1651 1650 0 17:03 ? 00:00:00 nginx: worker process nobody 1652 1650 0 17:03 ? 00:00:00 nginx: worker process nobody 1653 1650 0 17:03 ? 00:00:00 nginx: worker process nobody 1654 1650 0 17:03 ? 00:00:00 nginx: worker process root 1657 1460 0 17:05 pts/0 00:00:00 grep --color=auto nginx [root@localhost nginx112]# kill -s sighup 1650 [root@localhost nginx112]# ps -ef | grep nginx root 1650 1 0 17:03 ? 00:00:00 nginx: master process sbin/nginx nobody 1658 1650 0 17:06 ? 00:00:00 nginx: worker process nobody 1659 1650 0 17:06 ? 00:00:00 nginx: worker process nobody 1660 1650 0 17:06 ? 00:00:00 nginx: worker process nobody 1661 1650 0 17:06 ? 00:00:00 nginx: worker process root 1663 1460 0 17:06 pts/0 00:00:00 grep --color=auto nginx
通过./nginx + 参数 管理nginx:
reload :重新启动,(本质是给 master 发送一个hup 信号)
reopen :重新打开(本质发送 usr1 )
stop : 停止(发送 term 信号)
quit : Quit 信号