Worker进程 处理请求, Master进程管理Worker进程。
多进程模式避免了多线程共享同一个地址空间,某一个模块引发了段错误时,在地址越界出现时,会导致整个Nginx不可用。 因此Nginx采用多进程,在设计上保证了高可用。
建议Worker进程数量 = CPU核数。 Nginx 期望一个worker进程使用一颗cpu, 把某个worker进程和某个cpu绑定在一起,可以更好地使用cpu上的缓存,来减少缓存失效的命中率。
[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
[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
信号说明: 重识Nginx - 05 热部署_不停机更换新版本的nginx & nginx信号说明
[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]#
ng reload不会中断业务的使用, 新旧worker同时并存,其中旧worker仍然是原进程,它通过epoll_ctl将监听socket从epoll中移出,之前正在处理的TCP连接不受影响,当处理完(对于http请求,就是发完response,其他协议与语义相关)后旧worker正常退出。因此新建立的TCP连接就会由新worker处理。
重载配置文件
[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 ~]#
要求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 ~]#
kill -SIGTERM关闭worker进程后,会重新起一个worker进程. 该命令是worker提供给master的,通常管理员只需要操作master进程就可以,如果一定要操作worker进程,那么一定是可以确认某一个worker进程出问题了,且信号对应的功能可以解决该问题
master是worker进程的父进程,在Linux中,子进程退出时,会向父进程发送信号SIGCHLD,所以master进程可以感知到,这才能重新fork拉起新的worker子进程