On the problem of Nginx concurrency number: worker_connections,worker_processes and Max clients

Source: Internet
Author: User

I believe that a lot of people are like me, reading is not too careful and not too serious thinking, I feel that the book is supposed to be right, the recent reading I found that the things that were taken for granted in fact did not understand, the end result is that the book is someone else's, the knowledge in the book is others.

Whether it is a read nginx related to the book or on the web to see the Nginx configuration description of the article (http://wiki.nginx.org/EventsModule#worker_connections), without exception, in the Worker_ Connections and max_clients the relationship between the two concepts are consistent, especially when it comes to nginx as a reverse proxy max_clients calculation, are taken for granted max_clients = Worker_ Processes * WORKER_CONNECTIONS/4 This theory to calculate the formula. Since it's a theoretical formula, why divide it by 4? There must be a reason for it. I believe that some people know how to calculate, but a lot of people like me as a glance swept, really wait for others or ask themselves when the real feeling is foggy, do not know why.

I think, to find out whether the formula is correct, and how to calculate, then first of all the Nginx configuration instructions have a clear understanding:

Nginx as the HTTP server when:

max_clients = worker_processes * worker_connections

Nginx as the reverse proxy server:

max_clients = worker_processes * WORKER_CONNECTIONS/4

We will not determine whether the two formulas are correct, I used to think Nginx as a reverse proxy, to maintain both the client and back end of the proxy server two links should be divided by 2 instead of divided by 4, and later thought that the connection request is bidirectional, it is considered that dividing by 4 should be correct. Of course, I think the most likely is not correct, whether my judgment is correct ultimately, we need to judge each parameter according to the actual meaning (of course, it can be very clear to tell you that my idea is wrong.) As for the reason, I'll tell you later. )。 Let's take a look at the official notes of Max_clients,worker_processes and worker_connections, make clear the implications of what they contain, and then determine the relationship.

Worker_processes:

As described in the official English wiki configuration note, the number of processes that a person understands as a worker role (how many workers handle HTTP requests after Nginx starts).   Master does not process the request, but instead manages the worker process based on the corresponding profile information. The master process is primarily responsible for external Lanhuo (that is, requests received from clients), and the work is reasonably assigned to multiple workers, and each worker process is primarily responsible for working (processing requests)).

12345 syntax:worker_processes number | auto;default:worker_processes 1;context:mainDefines 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 (and not limited to) the number of CPU cores, the number of hard disk Drives that store data, and load pattern. When on doubt, setting it to the number of available CPU cores would be a good start (the value "Auto" would try to AutoDetect it).

The auto parameter is supported starting from versions 1.3.8 and 1.2.5.

The ideal worker_processes value depends on many factors, including but not limited to the number of cores in the CPU, the number of hard drives that store the data (what does it matter?). As with CPUs, there are cross-region read data problems), as well as load patterns (? What is this? When either of these factors is uncertain, setting it to CPU cores is probably a good initial value, and "Auto" is basically confirming a parameter value.

The "automatic" parameter value is supported from Nginx 1.3.8 and Nginx 1.2.5, automatic parameters can automatically detect the CPU cores and set the worker_processes parameters.

The following suggestions are also available online:

Nginx doesn ' t benefit from more than one worker per CPU.

One CPU configuration with more than one worker number, there is no benefit to nginx.

If Nginx is a doing cpu-intensive work such as SSL or gzipping and you had 2 or more cpus/cores, then your may set WORKER_PR Ocesses to is equal to the number of CPUs or cores.

If Nginx handles CPU-intensive (CPU-consuming) operations, it is recommended that this value be set to the number of CPUs or the number of cores of the CPU.

Worker_connections:

The official explanation is as follows, the individual considers the maximum number of connections (including all connections) that each worker process can concurrently process (initiate).

1234 syntax:worker_connections number;default:worker_connections 512;context:events

Sets the maximum number of simultaneous (concurrency) connections that can is opened by a worker process.

It should is kept in mind (remember) that this is the number includes all connections (e.g. connections with proxied servers (connection to proxy service) ), among others (between other roles), not just connections with clients (more than the number of connections to the client). Another consideration is, the actual (actual) number of simultaneous connections cannot exceed (over) the current limit (currently limited) On the maximum number of open files (maximum file opening), which can be changed by worker_rlimit_nofile (parameters that can be changed in Worker_rlimit_nofile ).

Worker_rlimit_nofile xxxxx;

Context:events

# # # #Specifies (specified) the value for maximum file descriptors (the maximum number of document descriptors that can be opened by a worker process) that can is opened by this process.

Note: After you set this, you cannot exceed the value of Worker_rlimit_nofile when you modify the Worker_connections value.

Max_clients:

This parameter does not appear in the Nginx configuration file, and I do not find this parameter in the official documentation, but many articles and books refer to this parameter. There are many people who translate this into the maximum number of visitors, the individual think there is nothing wrong, so we will be when this is nginx in the theoretical situation can handle the maximum number of visitors, of course, this customer number is not a specific user.

When nginx as the HTTP static content of the Web server, only need to handle the connection request from the client (the request is bidirectional, the connection is not the direction, so I said the reverse proxy is connected bidirectional, divided by 4 is incorrect).

A request is initiated by an HTTP client to create a TCP connection to the server-specified port (by default, port 80). The HTTP server listens to the client's request on that port. Once the request is received, the server returns a status to the client, such as "http/1.1" and the returned content, such as the requested file, error message, or other information. At the same time nginx in processing the HTTP request sent by the client should be just a connection, in theory, as the HTTP Web server role Nginx can handle the maximum number of connections is the maximum number of client connections.

So the theoretical maximum number of client connections is calculated as:

max_clients = worker_processes * worker_connections;

The problem that has kept me confused, how does nginx calculate the maximum number of client connections that can be handled as a reverse proxy?

If simply follow the HTTP server server's calculation mode, plus nginx to the user request to the proxy server established connection, the maximum number of client connections should also be I understand before, in Nginx as the HTTP server, based on the maximum number of client connections divided by 2. Of course, it's probably not the way I imagined it.

Official wiki (page markup is obsolete, but many articles on the web are quoted) see a explanation of why divided by 4:

If you are acting as a reverse proxy, the browser will open 2 connections to the server by default, and Nginx will use FDS (file descriptor) to establish a connection to the upstream backend from the same connection pool. The maximum number of connections is calculated as follows:

1 max_clients = worker_processes * worker_connections / 4;

Since (because) a browser opens 2 connections by default to a server and Nginx uses the FDS (file descriptors) from the same PO OL to connect to the upstream backend.

I have two questions:

    1. How does the browser know that Nginx is acting as a reverse proxy? What will be stated in the Nginx response?

    2. If the browser knows that Nginx is acting as a reverse proxy, then why do I need to open 2 connections to the server, and this root uses the file descriptor from the same connection pool (which is what), and back-end upstream to establish a connection what is the relationship?

These two problems solve the problem, nginx as the reverse proxy of the maximum number of client connections calculation is very clear.

2014.06.01 0:30 minute Update:

The Epoll model used by Nginx,

As a Web server, when processing HTTP requests, a worker process can be used to respond to a user request when acting as a Web service.

As a reverse proxy, the user sends a request to the Nginx,nginx to send the request to the backend by the proxy server, and the backend server gives Nginx,nginx the content back to the user.

Since the Epoll model does not wait, each step may be handled by a new connection, but this does not mean that nginx is required to divide by 4 as the maximum number of client connections for the reverse proxy.

2014.06.01 01:10 Update:

Http://wiki.nginx.org/EventsModule#worker_connections

Discussion on this issue in 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 CLI Ents 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). B UT that should is 2, not 4:i 

Agree with.

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.

Well, as discussed above, there is always a problem with the calculation, or we have a different understanding of max_clients.

If max_clients refers to the maximum number of client connections, then Nginx is the correct formula for the general Web server. If max_clients refers to the establishment of the maximum number of connections, because each browser looked at two concurrent connections, then nginx as a reverse proxy calculation formula is correct.

Personally, max_clients refers to the number of clients that Nginx can handle (the default is one client sending a request, and if the client is concurrently two requests, it can be divided by 2).

The final conclusion:

From the user's point of view, HTTP 1.1 protocol, because the browser uses two concurrent connections by default, so the calculation method:

Nginx as the HTTP server when:

max_clients = worker_processes * WORKER_CONNECTIONS/2

Nginx as the reverse proxy server:

max_clients = worker_processes * WORKER_CONNECTIONS/4

or from a general connection point of view: Customer concurrent connection is 1.

Nginx as the HTTP server when:

max_clients = worker_processes * worker_connections

Nginx as the reverse proxy server:

max_clients = worker_processes * WORKER_CONNECTIONS/2

When Nginx does a reverse proxy, it maintains a connection with the client and maintains a connection to the backend server.

Clients and number of users:

There is a difference between the clients (number of clients) and the number of users at the same time, when a user requests to send a connection, the two are equal, but when a user sends multiple connection requests by default, the number of clients is the number of users * The connection concurrency is sent by default.

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 is calculated by the following formula:

max_clients = worker_processes * worker_connections

But this does isn't equal to the number of users can is served at the same time by Nginx, since a lot of browsers Open 2 con Nections to the Web server at the same time.

An article on the optimization of nginx concurrency problem for high-flow load scenarios:

http://blog.martinfjordvald.com/2011/04/optimizing-nginx-for-high-traffic-loads/

Thank:

The "Elean Follow me" small partner in the Internet Operations Circle Group.

The internet on a variety of plagiarism and copied articles, although you repeatedly misled me, all kinds of pit me, but I still eventually skip all kinds of pits, step by step to understand the problem.

--------------------------

Reprinted from:

http://liuqunying.blog.51cto.com/3984207/1420556

On the problem of Nginx concurrency number: worker_connections,worker_processes and Max clients

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.