Nginx核心worker_processes、worker_connections设置

一、worker_processes、worker_connections设置多少合适?

worker_processes与worker_connections 设置好合适大小可以提高nginx处理性能,所以非常重要。

原作者的话

As a general rule you need the only worker with large number of
worker_connections, say 10,000 or 20,000.
However, if nginx does CPU-intensive work as SSL or gzipping and
you have 2 or more CPU, then you may set worker_processes to be equal
to CPU number.
Besides, if you serve many static files and the total size of the files
is bigger than memory, then you may increase worker_processes to
utilize a full disk bandwidth.
Igor Sysoev

翻译
一般一个进程足够了,你可以把连接数设得很大。如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。或者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加,以充分利用IO带宽(主要似乎是IO操作有block)。

二、worker_processes,工作进程数

  • 默认:worker_processes: 1
  • 调大:worker_processes: CPU核心数,(双核4线程,可以设置为4)

三、worker_connections,单个工作进程(worker)可以允许同时建立外部连接的数量

数字越大,能同时处理的连接越多

  • 默认:worker_connections: 1024
  • 调大:worker_connections: 100000,(调大到10万连接)

三、如何配置数值大小

系统的最大打开文件数>= worker_connections*worker_process

根据系统的最大打开文件数来调整,worker_connections进程连接数量要小于等于系统的最大打开文件数,worker_connections进程连接数量真实数量= worker_connections*worker_process

查看系统的最大打开文件数

[root@yhg-server ~]# ulimit -a|grep "open files"
open files                      (-n) 65535

四、在主配置文件nginx.conf中配置

user root;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;

# 工作进程数(我的服务器是2核,所以配置2)
worker_processes 2;

# 配置Nginx worker进程最大打开文件数
worker_rlimit_nofile 65535;

events {
    # 单个进程允许的客户端最大连接数(65535/2)
    worker_connections 32000;
}

五、max_clients

从用户的角度,http 1.1协议下,由于浏览器默认使用两个并发连接,因此计算方法:

  • nginx作为http服务器的时候:
    max_clients = worker_processes * worker_connections/2

  • nginx作为反向代理服务器的时候:
    max_clients = worker_processes * worker_connections/4

六、CPU相关信息查看

物理CPU个数

cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l

逻辑CPU个数:(核数)

cat /proc/cpuinfo | grep "processor" | wc -l

每个物理CPU中Core的个数:

cat /proc/cpuinfo | grep "cpu cores" | wc -l

查看CPU型号

cat /proc/cpuinfo | grep 'model name' |uniq

最后给大家送波福利

阿里云折扣快速入口

参考资料
nginx 并发数问题思考:worker_connections,worker_processes与 max clients

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是Nginx? Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器N...
    jiangmo阅读 2,585评论 1 9
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,079评论 0 9
  • 转载别人的文章,nginx调优。链接如下:http://blog.csdn.net/xifeijian/artic...
    Alex_ct阅读 985评论 0 1
  • 针对IPv4的内核7个参数的配置优化 这里提及的参数是和IPv4网络有关的Linux内核参数。我们可以将这些内核参...
    吃瓜的东阅读 891评论 0 0
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,895评论 0 3