nginx proxy websocket connection upper limit

 

1. Conclusion:

  1. Nginx can only maintain (65535*number of backend server IPs) long websocket connections at most. If there is only one backend websocket server IP, it can only support up to 65535 connections. The bottleneck is generated on nginx.

  2. It is recommended to use the DR mode of LVS for load balancing, so that the maximum number of long connections is only related to the websocket server resources (mainly memory), and a single websocket server can easily support millions of connections

 

 

2. Principle

 

The resources involved in using nginx as a reverse proxy for websocket are:

 

1. Memory (storage of related data structures), cpu, network

   The memory occupation is divided into two parts, one part is the memory occupied by the tcp protocol stack structure in the kernel, and the other part is the memory occupied by the bidirectional connection data structure in nginx

   According to the ideal situation, the data structure of a tcp connection occupies about 4KB in memory, the memory occupation of nginx, and there is no statistical related structure, here is equal to 2KB (nginx's memory utilization is very efficient, and there is a memory pool)

   

   For the current server, memory, cpu, and network will not be the bottleneck, so it will not be discussed here.

   

2. Number of file descriptors

   Kernel parameters may need to be adjusted, and the number of file descriptors is actually related to memory, because each time a tcp connection is opened, a file descriptor must be occupied.

   Kernel parameter: fs.file-max

   

   This is related to system resources and will not be a bottleneck

   

3. Number of port numbers

   The kernel parameters are: net.ipv4.ip_local_port_range, and the maximum value is 65535

   

   The linux kernel identifies a unique tcp connection through the four-tuple {local_ip, local_port, remote_ip, remote_port}.

   

   1) For the websocket server itself, local_ip and local_port are determined. If the memory and cpu are sufficient, it can support (the number of client_ip * 2^16) connections. That is to say, as long as the server resources are sufficient, it will not be a bottleneck.

   2) For the nginx server, local_ip and local_port are also determined. The difference is that it also acts as a client to connect to the websocket server, which occupies a port.

      Therefore, the maximum number of websocket connections that nginx can support is: (number of proxy websocket server IPs * 2^16), if there is only one websocket server IP, then there is only 65536, and if port 0 is removed, there is only 65535.

      

 

   

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326402875&siteId=291194637