nginx worker connections are not enough

How to fix NGINX : worker connections are not enough

Last updated on July 23rd, 2024 at 06:12 am

NGINX is a popular web server used by many high traffic websites and apps. It is capable of handling a large number of concurrent connections. Still sometimes you may get an error “worker connections are not enough” in NGINX. In this article, we will look at how to fix NGINX: worker connections are not enough error.

How many connections can NGINX handle?

Each NGINX worker can handle a maximum of 512 concurrent connections. In newer versions, NGINX supports up to 1024 concurrent connections, by default. However, most systems can handle more. Nevertheless, this configuration is sufficient for most websites. However, if your website gets too many visitors or if NGINX is not properly configured then you may get a “worker connections not enough” error.

Why You Get This Error?

There are a few reasons why your users may get ‘worker connections are not enough’ error. Let us look at some of them:

  1. Faulty Configuration : One of the most common ones is that your NGINX server is unable to handle number of incoming connections and you need to increase this limit. We will look at how to solve this problem in this article.
  2. Redirect Loops : You may also get this error if there is an infinite redirect loop that keeps hitting your server with new redirect requests thereby crossing the connection limit.
  3. Faulty Code : If there is a bug in your code, it may result in too many requests being sent to your server. Such issues can be present on your client side as well as server side.
  4. DOS/DDOS Attack: In this case, a malicious attacker or bot may keep sending too many requests to your server thereby causing this problem. In this case, you need to identify the IP or group of IPs sending so many requests and update your firewall to block them right away.
  5. XSS Attack : XSS attack is when a malicious piece of code has been inserted into your database via form inputs such as textboxes. When this value is retrieved from your database and run client side, it may send too many requests to your server, causing this problem. In this case, make sure you validate form inputs before storing them in database.

How to fix NGINX : worker connections are not enough

To fix this problem, you will need to update the worker_connections directive in your NGINX server configuration. Here are the steps to fix worker connections are not enough error.

1. Open NGINX configuration

Open terminal and run the following command to open NGINX configuration file.

$ sudo vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/example.conf then open its configuration with the following command

$ sudo vi /etc/nginx/sites-enabled/example.conf

2. Increase Worker Connections

Add the following events block with worker_connections directive to increase the number of worker connections to 2048. You can increase this number if you want. worker_connections determines the maximum number of connections to be supported by your server.

events {
...
   worker_connections 2048;
...
}

Please note, the number of workers are limited by the amount of memory available on your server. Also, as the number of workers increase, so will the memory consumption of NGINX server.

Alternatively, you can also try to limit the number of requests processes by NGINX server.

3. Restart NGINX Server

Finally, run the following command to check syntax of your updated config file.

$ sudo nginx -t

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

If you happen to get this error, it is advisable to thoroughly check the server configuration, before increasing the number of worker connections. The default value of worker_connections directive is enough for most websites.

Conclusion

In this article, we have learnt how to fix ‘too many connections’ error in NGINX server. You can do this by simply modifying worker_connection server directive in NGINX configuration. But the default connection limit is more than enough to handle most of the websites and apps. Unless your website is getting seriously high traffic, it is advisable to check your server configuration first for any problems. If that looks fine, go through your server logs and find out the request URL(s) causing this problem. Once you identified them, check their client code as well as server code to see if there are any bugs while requesting these URLs from front end, or while processing them at back end.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!

Also read
How to Enable HTTP/2 in NGINX
How to Disable ETag in NGINX
Apache Restrict URL By IP
How to Enable Multi Factor Authentication in AWS