connection of the key concepts of Nginx

What connection that?
  tcp connection is connected to the package, including the socket connection and read events, write events. Use connection, we can easily establish a connection, send data and receive data, we can deal with any back-end service. Which, Nginx http request processing is based on the connection.

How Nginx via a connection process?

  1. Nginx at startup, parses the configuration file to give the desired listening port and IP address.
  2. In the master in the process of initializing the socket good monitoring (ie create socket, set addrreuse etc., bound to the specified ip address port, and then listen)
  3. fork out multiple sub-processes, the child who will compete accept the new connection.
  4. The client initiates a connection to Nginx
  5. The client and server through the three-way handshake to establish a connection
  6. A sub-process successfully accept the connection, the connection has been socket (socket is an interface that acts as a middleman between the user process and the TCP / IP protocol, complete TCP / IP protocol writing, users only need to understand the interface can be)
  7. The created child process of Nginx package connections, i.e. ngx_connection_t structure
  8. Set the time to read and write handler, add an event to read and write data to interact with the client
  9. Nginx or turn off the connected client active

How to deal with Nginx as a client connection request other services?

  1. Nginx first get a ngx_connection_t structure
  2. Create a socket and set the properties of socket (such as non-blocking)
  3. Add an event to read and write
  4. Call connect / read / write to invoke connection
  5. Turn off connection
  6. Release ngx_connection_t

Nginx maximum number of connections can be established is how much?

  Nginx by setting worker_connectons to set the maximum number of connections per process support, and if the value is greater than nofile, then the actual maximum number of connections is nofile (operating system, a process can open fd (fd file descriptor fd, as the process file open pointer entry) the maximum number can be obtained by ulimit -n command. a socket occupy a fd, fd when run out, and then create a socket will fail).

  Each worker process has a separate connection pool when (a ngx_connection_t array structure worker_connections size), Nginx to save all idle ngx_connection_t through a list free_connections, each get a connection, obtain one from free_connections, after use and then put back in the free list.

  Overall, the http request for a local resource, the maximum number of concurrent support is worker_connections * worker_processes, and if it is http as a reverse proxy, the maximum number of concurrent to worker_connections * worker_processes / 2, because the reverse proxy server each will establish concurrent client connections and links to back-end services, will occupy two links.

 

Guess you like

Origin www.cnblogs.com/smallzhen/p/12623585.html