Introduction
When Nginx experiences traffic volumes exceeding its configured connection capacity, the error log fills with "worker_connections are not enough" messages. This typically occurs during DDoS attacks, flash crowd events, or viral traffic spikes. The exact error reads:
2026/04/08 15:42:33 [alert] 2345#0: 1024 worker_connections are not enoughNginx silently drops excess connections, resulting in failed requests for users.
Symptoms
- Nginx error log shows "worker_connections are not enough" alert messages
- Clients receive connection refused or connection reset errors
ss -sshows a high number of established connections approaching system limits- Nginx accepts connections but fails to serve them, causing HTTP 502 or connection resets
- Server load averages spike while response throughput drops
Common Causes
- Default
worker_connections 1024is insufficient for high-traffic scenarios - OS file descriptor limit (
nofile) caps the number of connections Nginx can handle - Each connection consumes a file descriptor, and worker_connections must not exceed the OS limit
- Connection backlog queue overflows when all workers are saturated
- Insufficient worker processes for available CPU cores
Step-by-Step Fix
- 1.Increase worker_connections and worker_processes in
/etc/nginx/nginx.conf: - 2.```nginx
- 3.events {
- 4.worker_connections 4096;
- 5.multi_accept on;
- 6.use epoll;
- 7.}
worker_processes auto;
``
With worker_processes auto and worker_connections 4096`, Nginx can handle approximately 4096 x CPU cores simultaneous connections.
- 1.Raise the OS file descriptor limit. Edit
/etc/security/limits.conf: - 2.
` - 3.nginx soft nofile 65535
- 4.nginx hard nofile 65535
- 5.www-data soft nofile 65535
- 6.www-data hard nofile 65535
- 7.
` - 8.Replace
www-datawith your Nginx worker user. - 9.Set the system-wide file descriptor limit in
/etc/sysctl.conf: - 10.
` - 11.fs.file-max = 2097152
- 12.fs.nr_open = 1048576
- 13.net.core.somaxconn = 65535
- 14.net.ipv4.tcp_max_syn_backlog = 65535
- 15.
` - 16.Apply with
sudo sysctl -p. - 17.Configure the systemd service override for Nginx. Create
/etc/systemd/system/nginx.service.d/limits.conf: - 18.```ini
- 19.[Service]
- 20.LimitNOFILE=65535
- 21.
` - 22.Then reload systemd:
sudo systemctl daemon-reload. - 23.Enable connection rate limiting to mitigate DDoS impact:
- 24.```nginx
- 25.http {
- 26.limit_conn_zone $binary_remote_addr zone=addr:10m;
- 27.limit_req_zone $binary_remote_addr zone=req:10m rate=10r/s;
server { limit_conn addr 100; limit_req zone=req burst=20 nodelay; } } ```
- 1.Reload Nginx and verify:
- 2.```bash
- 3.sudo systemctl reload nginx
- 4.sudo nginx -T | grep worker_connections
- 5.cat /proc/$(cat /var/run/nginx.pid)/limits | grep "open files"
- 6.
`
Prevention
- Monitor active connections using the Nginx stub_status module at
/nginx_status - Set up alerts when connections exceed 70% of worker_connections capacity
- Deploy a CDN (Cloudflare, AWS CloudFront) to absorb DDoS traffic before it reaches Nginx
- Configure fail2ban or Nginx geo-blocking for known malicious IP ranges
- Keep
sysctltuning documented and version-controlled for quick disaster recovery - Use
ulimit -n 65535in any custom startup scripts to ensure limits are applied