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?
- Nginx at startup, parses the configuration file to give the desired listening port and IP address.
- 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)
- fork out multiple sub-processes, the child who will compete accept the new connection.
- The client initiates a connection to Nginx
- The client and server through the three-way handshake to establish a connection
- 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)
- The created child process of Nginx package connections, i.e. ngx_connection_t structure
- Set the time to read and write handler, add an event to read and write data to interact with the client
- Nginx or turn off the connected client active
How to deal with Nginx as a client connection request other services?
- Nginx first get a ngx_connection_t structure
- Create a socket and set the properties of socket (such as non-blocking)
- Add an event to read and write
- Call connect / read / write to invoke connection
- Turn off connection
- 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.