worker_processes:
官方英文版wiki配置說明中的描述如下,個人理解為worker角色的進程個數(nginx啟動后有多少個worker處理http請求。master不處理請求,而是根據相應配置文件信息管理worker進程. master進程主要負責對外攬活(即接收客戶端的請求),並將活兒合理的分配給多個worker,每個worker進程主要負責干活(處理請求))。
1
2
3
4
5
|
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進程能並發處理(發起)的最大連接數(包含所有連接數)。
1
2
3
4
|
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后端。則最大連接數的計算公式如下:
1
|
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.
http://blog.51cto.com/liuqunying/1420556