我相信,不少人都跟我同样,看书都不会太细致也不太认真思考,感受书中讲的东西都应该是对的,最近读书时我发现之前认为理所固然的东西事实上压根都没有弄明白,最终的结果是,书是别人的,书中的知识也是别人的。html
不管是看过的nginx有关书仍是网上看到的有关nginx 配置说明的文章(http://wiki.nginx.org/EventsModule#worker_connections),无一例外,在讲到 worker_connections 和 max_clients这两个概念的关系时都一致的一笔带过,尤为是在讲到nginx做为反向代理时max_clients的计算时,都是想固然的贴出max_clients = worker_processes * worker_connections/4这个理论计算公式来。既然是理论公式,那么为何要除以4呢?确定是有缘由的吧。我相信有些人是知道如何计算的,可是不少人都如我同样一眼扫过,真正等到别人或者本身问本身的时候就真的感受是云里雾里,不知因此然了。nginx
我认为,要搞清楚这个公式是否正确,以及如何计算的,那首先要对nginx的各个配置说明有清晰的认识:web
nginx做为http服务器的时候:后端
max_clients = worker_processes * worker_connections浏览器
nginx做为反向代理服务器的时候:bash
max_clients = worker_processes * worker_connections/4服务器
咱们暂且不来断定这两个公式是否正确,我曾经认为nginx做为反向代理,要同时维持客户端和后端被代理server两个连接应该是除以2而不是除以4,后来想到链接请求都是双向的,也就认为除以4应该是正确的。固然,我想的极有多是不正确的,个人判断是否正确最终仍是须要咱们根据每个参数的实际含义来判断(固然,能够很明确的告诉你,个人想法是错误的。至于缘由呢,后面告诉你。)。仍是让咱们一块儿来看下max_clients,worker_processes和worker_connections的官方说明,对其所包含的含义搞清楚明白,再来肯定之间的关系。并发
worker_processes:app
官方英文版wiki配置说明中的描述以下,我的理解为worker角色的进程个数(nginx启动后有多少个worker处理http请求。master不处理请求,而是根据相应配置文件信息管理worker进程. master进程主要负责对外揽活(即接收客户端的请求),并将活儿合理的分配给多个worker,每一个worker进程主要负责干活(处理请求))。运维
syntax:worker_processes number | auto; default: worker_processes 1; context:main Defines the number of worker processes.
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
root 3153 1 0 12:11 ? 00:00:00 nginx: master process
nobody 3154 3153 0 12:11 ? 00:00:00 nginx: worker process nobody 3155 3153 0 12:11 ? 00:00:00 nginx: worker process
nobody 3156 3153 0 12:11 ? 00:00:00 nginx: worker process
nobody 3157 3153 0 12:11 ? 00:00:00 nginx: worker process
The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it).
The auto parameter is supported starting from versions 1.3.8 and 1.2.5.
最理想的worker_processes值取决于不少因素,包含但不限于CPU的核数,存储数据的硬盘驱动器个数(跟这个有什么关系?难道和cpu同样,存在跨区域读取数据问题),以及负载模式(?这个是什么?)当其中任何一个因素不肯定的时候,将其设置为cpu核数或许是一个比较好的初始值,“自动”也基本是如此确认一个参数值的。
“自动”这个参数值是从nginx 1.3.8和nginx 1.2.5 开始进行支持的,自动参数能够自动检测 cpu cores 并设置 worker_processes 参数 。
在网上也看到如下建议:
nginx doesn't benefit from more than one worker per CPU.
一个cpu配置多于一个worker数,对nginx而言没有任何益处。
If Nginx is doing CPU-intensive work such as SSL or gzipping and you have 2 or more CPUs/cores, then you may set worker_processes to be equal to the number of CPUs or cores.
若是nginx处理的是cpu密集型(比较耗费cpu的)的操做,建议将此值设置为cpu个数或cpu的核数。
worker_connections:
官方解释以下,我的认为是每个worker进程能并发处理(发起)的最大链接数(包含全部链接数)。
syntax:worker_connections number; default: worker_connections 512; context:events
Sets the maximum number of simultaneous(并发) connections that can be opened by a worker process.
It should be kept in mind(谨记) that this number includes all connections (e.g. connections with proxied servers(与被代理服务之间的链接数), among others(与其余角色之间的)), not only connections with clients(不只仅是与客户端之间的链接数). Another consideration is that the actual(实际的) number of simultaneous connections cannot exceed(超过) the current limit(当前限制) on the maximum number of open files(最大文件打开数), which can be changed by worker_rlimit_nofile (能够在worker_rlimit_nofile中改变的参数).
worker_rlimit_nofile xxxxx;
context:events
####Specifies(指定) the value for maximum file descriptors(可被一个工做进程打开的最大文件描述符数量) that can be opened by this process.
注意:设置了这个后,修改worker_connections值时,是不能超过worker_rlimit_nofile的这个值。
max_clients:
这个参数没有出如今nginx的配置文件中,我也没在官方的文档中找到这个参数,可是不少的文章和书籍都提到了这个参数。有不少人将这个翻译为最大访问客户数,我的认为没有什么不妥的,所以咱们就当这个是nginx在理论状况下能处理的最大访问客户数,固然这个客户数不是具体的用户。
当nginx做为http 静态内容的web服务器时,只须要处理来自客户端的链接请求便可(请求是双向的,链接是没有方向的,因此我上面说的反向代理是链接双向,除以4的说法是不正确的)。
由HTTP客户端发起一个请求,建立一个到服务器指定端口(默认是80端口)的TCP链接。HTTP服务器则在那个端口监听客户端的请求。一旦收到请求,服务器会向客户端返回一个状态,好比"HTTP/1.1 200 OK",以及返回的内容,如请求的文件、错误消息、或者其它信息。同一时刻nginx在处理客户端发送的http请求应该只是一个connection,由此可知理论上做为http web服务器角色的nginx可以处理的最大链接数就是最大客户端链接数。
所以理论上的最大客户端链接数计算公式为:
max_clients = worker_processes * worker_connections;
那一直让我犯迷糊的问题,nginx做为反向代理时能够处理的最大客户链接数是如何计算的呢?
若是只是简单的按照http server服务器的计算模式,加上nginx将用户请求转向被代理服务器时创建的链接,最大的客户链接数也应该仍是我以前理解的,在nginx做为http服务器的最大客户端链接数的基础上除以2了。固然,事实上很可能和我想象的不同了。
官方wiki(页面标记已通过时,可是网上不少文章都在引用)看到一个关于为何除以4的解释:
若是做为反向代理,由于浏览器默认会开启2个链接到server,并且Nginx还会使用fds(file descriptor)从同一个链接池创建链接到upstream后端。则最大链接数的计算公式以下:
max_clients = worker_processes * worker_connections / 4;
Since(由于) a browser opens 2 connections by default to a server and nginx uses the fds (file descriptors) from the same pool to connect to the upstream backend。
我有两个疑问:
浏览器怎么知道nginx是做为反向代理的?nginx 响应中会有说明?
若是浏览器知道nginx是做为反向代理的,那为何须要开启2个链接到server,并且这根使用文件描述符从同一个链接池(这又是什么东东),与后端upstream创建链接有什么关系?
这两个问题解决了,nginx做为反向代理的最大客户链接数的计算也就很明确了。
2014.06.01 0:30分更新:
nginx使用的epoll模型,
做为web server时,在处理http请求时,若是做为web服务器,一个worker进程就能够用来响应一个用户请求。
做为反向代理时,用户发送请求到nginx,nginx发送请求到后端被代理服务器,后端服务器相应给nginx,nginx将内容返回给用户。
因为epoll模型是不等待的,每一步都有多是由新建链接处理的,可是这也不能说明nginx做为反向代理最大客户链接数是须要除以4的。
2014.06.01 01:10更新:
http://wiki.nginx.org/EventsModule#worker_connections
2011年你们对此问题的讨论:
http://mailman.nginx.org/pipermail/nginx/2011-February/024979.html
Antoine BONAVITA antoine_bonavita at yahoo.com
Thu Feb 3 11:43:22 MSK 2011
Previous message: Calculating the max. clients by worker_connections
Next message: Calculating the max. clients by worker_connections
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I would say "and nginx uses the fds (file descriptors) from the same pool to
connect to the upstream backen" (wiki quote). But that should be 2, not 4: I
agree with you on this.
If any of the gurus out there could shed light on this, I'm sure a lot of us
would appreciate.
Antoine.
Ryan Chan ryanchan404 at gmail.com
Thu Feb 3 19:38:49 MSK 2011
Previous message: Calculating the max. clients by worker_connections
Next message: Calculating the max. clients by worker_connections
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In fact,
As normal web server, the maths would also be
max_clients = worker_processes * worker_connections / 2
since every browser opens 2 connections by default,
since browser uses 2 connections in HTTP 1.1.
好吧,如上讨论内容,总有一个计算方式是有问题的,或者说你们对max_clients的理解不太同样。
若是说max_clients指的是最大客户端链接数,那nginx做为通常web server的计算公式是正确的。若是max_clients指的是创建链接最大客户数,因为每个浏览器看了两个并发链接,那么nginx做为反向代理的计算公式就是正确的。
我的认为,max_clients 指nginx能够处理的客户端数(默认一个客户端发送一个请求,若是客户端并发两个请求,那就只能再除以2了)。
最终的结论:
从用户的角度,http 1.1协议下,因为浏览器默认使用两个并发链接,所以计算方法:
nginx做为http服务器的时候:
max_clients = worker_processes * worker_connections/2
nginx做为反向代理服务器的时候:
max_clients = worker_processes * worker_connections/4
或者从通常创建链接的角度:客户并发链接为1.
nginx做为http服务器的时候:
max_clients = worker_processes * worker_connections
nginx做为反向代理服务器的时候:
max_clients = worker_processes * worker_connections/2
nginx作反向代理时,和客户端之间保持一个链接,和后端服务器保持一个链接。
clients与用户数:
同一时间的clients(客户端数)和用户数仍是有区别的,当一个用户请求发送一个链接时这两个是相等的,可是当一个用户默认发送多个链接请求的时候,clients数就是用户数*默认发送的链接并发数了。
Calculating the max client in Nginx
Answer:
If you use Nginx as a web server, the max. number of client can be served at the same time by Nginx should be calculated by the following formula:
max_clients = worker_processes * worker_connections
But this does not equal to the number of users can be served at the same time by Nginx, since a lot of browsers open 2 connections to the web server at the same time.
有关高流量负载状况下,nginx并发问题优化的一个文章:
http://blog.martinfjordvald.com/2011/04/optimizing-nginx-for-high-traffic-loads/
感谢:
互联网运维圈子群中的“Elean follow me”小伙伴.
互联网上各类抄袭和被抄袭的文章,虽然大家一再地误导我,各类坑我,可是我仍是最终跳过各类坑,一步一步把问题搞明白了。