The solution to the worker connections problem in Nginx

  • 2020-05-13 04:31:37
  • OfStack

Check the log, there is 1 [warn]: 3660#0: 20000 worker_connections are more open file resource limit: 1024!!

After installing nginx, the default maximum number of concurrences is 1024. If your website has too many visitors, and the number of concurrences is far beyond 1024, then you need to modify the value of worker_connecions. The higher the value is, the higher the number of concurrences will be. Of course, you have to do it on your own, and you can't set it too big to make your CPU run 100%.

So, when you modify the worker_connections value in the configuration file, and then restart nginx, you will find in the log that the first warn warning we mentioned is basically: 20,000 concurrent connections have exceeded the resource limit for opening the file :1024! In this case, we would modify the configuration file to add 1 line to remove the restriction, as if it were ServerLimit in apache.

Open the configuration file and add this line above the "event" line:

worker_rlimit_nofile xxxxx; ####Specifies the value for maximum file descriptors that can be opened by this process.

Note: after setting this, when you modify the worker_connections value, you cannot exceed the worker_rlimit_nofile value, otherwise you will be prompted by the warn.

Save the configuration file, exit and restart nginx.

If the value of worker_connections in nginx is set to 1024 and the value of worker_processes is set to 4, according to the theoretical calculation formula for the maximum number of connections in reverse proxy mode:

Maximum number of connections = worker_processes * worker_connections/4

According to the relevant information, the recommended value of worker_connections in the production environment should be higher than 9000. It is planned to set one nginx to 10240 and observe it for another period of time.


Related articles: