24 Nginx全局块的工作进程的两个指令

24 Nginx全局块的工作进程的两个指令

24.1 进程

nginx的进程模型:1.管理员、2.master进程、3.worker进程、4.用户

image

1.master 进程管理所有的 worker 进程

2.worker 进程可以用来接收和处理用户的请求

提问:1.master 进程一定要创建 worker 进程吗?2.master 进程能创建多少 worker 进程?

24.2 master_process 指令

master_process:用来指定是否开启工作进程(默认为 on )

语法 master_process on|off;
默认值 master_process on;
位置 全局块

 

 

 

[root@nginx-100 ~]# ps -ef|grep nginx
root       1466      1  0 21:40 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1467   1466  0 21:40 ?        00:00:00 nginx: worker process
root       1471   1431  0 21:43 pts/0    00:00:00 grep --color=auto nginx
# 修改配置
[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
..........
user  www;
master_process off;
worker_processes  1;
..........
# 热加载后,未关闭工作进程?
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx-100 ~]# ps -ef|grep nginx
root       1466      1  0 21:40 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1477   1466  0 21:44 ?        00:00:00 nginx: worker process
root       1479   1431  0 21:45 pts/0    00:00:00 grep --color=auto nginx
# 需要停止nginx,再启动nginx 才会生效
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx
[root@nginx-100 ~]# ps -ef|grep nginx
root       1482      1  0 21:45 ?        00:00:00 /usr/local/nginx/sbin/nginx
root       1484   1431  0 21:45 pts/0    00:00:00 grep --color=auto nginx

注意:master_process 参数需要先 stop 再 start 才会生效

24.3 worker_process 指令

worker_process: 用于配置Nginx生成工作进程的数量,是Nginx服务器实现并发处理服务的关键所在,理论上worker_process 配置值越大,可支持并发处理量也越多,但实际上这个值的设定需受到服务器自身限制,建议将该值和服务器CPU的内核数保持一致(默认为1)

语法 worker_process num/auto;
默认值 1
位置 全局块

 

 

 

 

[root@nginx-100 ~]# ps -ef|grep nginx
root       1482      1  0 21:45 ?        00:00:00 /usr/local/nginx/sbin/nginx
root       1493   1431  0 21:59 pts/0    00:00:00 grep --color=auto nginx
# 注意要打开master_process,否则即使修改 worker_processes 也不生效
[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
..........
user  www;
master_process on;
worker_processes  2;
..........
# 同样因为修改master_process 要stop 再start
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx-100 ~]# ps -ef|grep nginx
root       1482      1  0 21:45 ?        00:00:00 /usr/local/nginx/sbin/nginx
root       1498   1431  0 21:59 pts/0    00:00:00 grep --color=auto nginx
# worker_processes  2 变为了2个,与 服务器核心数保持一致
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@nginx-100 ~]# /usr/local/nginx/sbin/nginx
[root@nginx-100 ~]# ps -ef|grep nginx
root       1501      1  0 21:59 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www        1502   1501  0 21:59 ?        00:00:00 nginx: worker process
www        1503   1501  0 21:59 ?        00:00:00 nginx: worker process
root       1505   1431  0 21:59 pts/0    00:00:00 grep --color=auto nginx
[root@nginx-100 ~]# lscpu 
..........
CPU(s):                2
..........

注意:worker_process 默认为1,设置与服务核心数一致,将尽量扩大性能,auto 是自动设置 worker_process 与服务器核心数保持一致

24.4 总结

1.master 进程一定要创建 worker 进程吗?

  可以通过 master_process 参数设置是否创建 worker 进程,一般默认 on 开启,做某些测试时可能会关掉

2.master 进程能创建多少 worker 进程?

  受限于服务器核心数,建议与服务器核心数保持一致,设置为 auto

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

                                                                                                                         无敌小马爱学习

 

posted on 2026-03-25 12:18  马俊南  阅读(6)  评论(0)    收藏  举报