Sobes.tech
Junior — Middle

What to do if the number of active connections with nginx exceeds 1024 and the service stops working stably?

sobes.tech AI

Answer from AI

If the number of active connections to nginx exceeds 1024 and the service starts to become unstable, it is most likely due to reaching the limit of open file descriptors or the connection limit in the system.

What to do:

  1. Increase the open files limit (ulimit):

    • Check the current limit with the command ulimit -n.
    • Increase the limit in the system configuration (/etc/security/limits.conf) and for the nginx process.
  2. Configure nginx parameters:

    • Increase worker_connections in the nginx config (for example, to 4096 or higher).
    • Ensure that worker_rlimit_nofile is set sufficiently high.
  3. Check system limits:

    • Kernel parameters such as fs.file-max (maximum number of open files in the system).
    • Configure sysctl if necessary.
  4. Optimize nginx configuration:

    • Use keepalive connections with reasonable timeouts.
    • Enable event model (e.g., epoll on Linux).
  5. Monitoring and scaling:

    • If the load is constantly high, consider horizontal scaling (load balancers, multiple nginx instances).

Example of increasing limits in nginx config:

worker_rlimit_nofile 65535;
events {
    worker_connections 4096;
}

And in the system (for example, in /etc/security/limits.conf):

nginx soft nofile 65535
nginx hard nofile 65535

After changes, restart nginx and check the status.