重识Nginx - 14 Nginx 多进程结构

简介: 重识Nginx - 14 Nginx 多进程结构

20200103193054943.png

Nginx的请求处理流程


5023e958624f42a0a76034c6428d8d29.png

Nginx的多进程结构


ffe9f9b783584c98a55a656698230ca9.png


Worker进程 处理请求, Master进程管理Worker进程。


多进程模式避免了多线程共享同一个地址空间,某一个模块引发了段错误时,在地址越界出现时,会导致整个Nginx不可用。 因此Nginx采用多进程,在设计上保证了高可用。


建议Worker进程数量 = CPU核数。 Nginx 期望一个worker进程使用一颗cpu, 把某个worker进程和某个cpu绑定在一起,可以更好地使用cpu上的缓存,来减少缓存失效的命中率。


Nginx进程结构演示

关键配置

[root@VM-0-7-centos artisan_ng]# cat /root/ng/artisan_ng/conf/nginx.conf |grep worker_processes
worker_processes  2;
[root@VM-0-7-centos artisan_ng]#


这里我们设置的 worker_processes 为 2

查看ng进程

[root@VM-0-7-centos artisan_ng]# ./sbin/nginx -c ./conf/nginx.conf
[root@VM-0-7-centos artisan_ng]#
[root@VM-0-7-centos artisan_ng]# ps -ef|grep nginx |grep -v grep
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      994793  994792  0 23:10 ?        00:00:00 nginx: worker process
root      994794  994792  0 23:10 ?        00:00:00 nginx: worker process
root      994795  994792  0 23:10 ?        00:00:00 nginx: cache manager process
root      994796  994792  0 23:10 ?        00:00:00 nginx: cache loader process
[root@VM-0-7-centos artisan_ng]#


可以看到worker process 有2个 。

其中master的pid 为 994792 , 两个worker进程是由master进程启动的, 其中 pid 分别为 994793 和 994794


b0056d22abd448baac1116986568e0ca.png


信号说明

信号说明: 重识Nginx - 05 热部署_不停机更换新版本的nginx & nginx信号说明


11e362c2ada94d47bf6e5e7adf6765ad.png


reload 观察 worker进程的pid

[root@VM-0-7-centos ~]# ps -ef|grep nginx
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      994793  994792  0 23:10 ?        00:00:00 nginx: worker process
root      994794  994792  0 23:10 ?        00:00:00 nginx: worker process
root      994795  994792  0 23:10 ?        00:00:00 nginx: cache manager process
root      996157  996087  0 23:19 pts/5    00:00:00 grep --color=auto nginx
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# cd /root/ng/artisan_ng
[root@VM-0-7-centos artisan_ng]# ./sbin/nginx -s reload
[root@VM-0-7-centos artisan_ng]# ps -ef|grep nginx |grep -v grep
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      996231  994792  0 23:20 ?        00:00:00 nginx: worker process
root      996232  994792  0 23:20 ?        00:00:00 nginx: worker process
root      996233  994792  0 23:20 ?        00:00:00 nginx: cache manager process
[root@VM-0-7-centos artisan_ng]#
[root@VM-0-7-centos artisan_ng]#


0c0c12bc08174fe9b6a3dec989365cb2.png


ng reload不会中断业务的使用, 新旧worker同时并存,其中旧worker仍然是原进程,它通过epoll_ctl将监听socket从epoll中移出,之前正在处理的TCP连接不受影响,当处理完(对于http请求,就是发完response,其他协议与语义相关)后旧worker正常退出。因此新建立的TCP连接就会由新worker处理。


向Master 进程 发送 SIGHUB

重载配置文件

[root@VM-0-7-centos ~]# ps -ef|grep nginx |grep -v grep
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      996231  994792  0 23:20 ?        00:00:00 nginx: worker process
root      996232  994792  0 23:20 ?        00:00:00 nginx: worker process
root      996233  994792  0 23:20 ?        00:00:00 nginx: cache manager process
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# kill -SIGHUP 994792
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# ps -ef|grep nginx |grep -v grep
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      997941  994792  0 23:32 ?        00:00:00 nginx: worker process
root      997942  994792  0 23:32 ?        00:00:00 nginx: worker process
root      997943  994792  0 23:32 ?        00:00:00 nginx: cache manager process
[root@VM-0-7-centos ~]#


2c934233d58a4c6a8921e80480376708.png


向worker进程 发送 SIGTERM

要求Nginx立刻关闭服务


[root@VM-0-7-centos ~]# ps -ef|grep nginx |grep -v grep
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      997941  994792  0 23:32 ?        00:00:00 nginx: worker process
root      997942  994792  0 23:32 ?        00:00:00 nginx: worker process
root      997943  994792  0 23:32 ?        00:00:00 nginx: cache manager process
[root@VM-0-7-centos ~]# ^C
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# kill -SIGTERM  997942
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]#
[root@VM-0-7-centos ~]# ps -ef|grep nginx |grep -v grep
root      994792       1  0 23:10 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
root      997941  994792  0 23:32 ?        00:00:00 nginx: worker process
root      997943  994792  0 23:32 ?        00:00:00 nginx: cache manager process
root      998267  994792  0 23:34 ?        00:00:00 nginx: worker process
[root@VM-0-7-centos ~]#


46296779d6c344258309e634e694325a.png


kill -SIGTERM关闭worker进程后,会重新起一个worker进程. 该命令是worker提供给master的,通常管理员只需要操作master进程就可以,如果一定要操作worker进程,那么一定是可以确认某一个worker进程出问题了,且信号对应的功能可以解决该问题


master是worker进程的父进程,在Linux中,子进程退出时,会向父进程发送信号SIGCHLD,所以master进程可以感知到,这才能重新fork拉起新的worker子进程

相关文章
|
4月前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
113 0
|
10月前
|
应用服务中间件 nginx Windows
windows下DOS命令杀掉Nginx应用进程
windows下DOS命令杀掉Nginx应用进程
138 1
|
1月前
|
运维 应用服务中间件 网络安全
运维系列.Nginx配置文件结构功能总结
运维系列.Nginx配置文件结构功能总结
39 0
运维系列.Nginx配置文件结构功能总结
|
2月前
|
应用服务中间件 nginx
cmd 杀掉 nginx后台进程 命令杀掉nginx后台 nginx 常用命令
cmd 杀掉 nginx后台进程 命令杀掉nginx后台 nginx 常用命令
128 0
|
2月前
|
缓存 应用服务中间件 开发工具
Ngnix配置文件nginx.conf的文件结构
Ngnix配置文件nginx.conf的文件结构
|
4月前
|
存储 应用服务中间件 nginx
Nginx模块开发:模块结构的源码阅读以及过滤器(Filter)模块的实现
Nginx模块开发:模块结构的源码阅读以及过滤器(Filter)模块的实现
163 0
|
4月前
|
应用服务中间件 nginx
Nginx源码阅读:nginx_shmtx共享互斥锁(进程锁)
Nginx源码阅读:nginx_shmtx共享互斥锁(进程锁)
105 0
|
4月前
|
监控 应用服务中间件 nginx
Supervisor快速入门 | 使用Supervisor守护Nginx进程
Supervisor快速入门 | 使用Supervisor守护Nginx进程
162 0
|
4月前
|
缓存 负载均衡 算法
百度搜索:蓝易云【如何优化Nginx服务进程详细。
优化Nginx服务进程是一个持续的过程,需要不断地监测和调整。建议在优化之前备份配置文件,并逐步应用和测试每个优化步骤的效果,以确保服务的稳定性和可靠性。
49 0
|
11月前
|
应用服务中间件 nginx
【Nginx异常】Nginx启动一闪而过没反应,Nginx双击打开后,没有启动成功,也没有进程,且127.0.0.1:8080访问不到
【Nginx异常】Nginx启动一闪而过没反应,Nginx双击打开后,没有启动成功,也没有进程,且127.0.0.1:8080访问不到
771 0