Likely causes
- •The worker limit is still the default of 1024. Many distributions start nginx with a soft limit of 1024 descriptors. Check with: grep 'Max open files' /proc/<worker pid>/limits. If it shows 1024 and worker_connections is higher, this is the cause.
- •worker_rlimit_nofile is not set, or is lower than worker_connections needs. A proxied request uses two descriptors (client and upstream), and static files use more. If worker_rlimit_nofile is missing from nginx.conf, or is less than twice worker_connections, workers run out first under load.
- •You raised limits in /etc/security/limits.conf, but nginx runs under systemd. limits.conf applies to login sessions, not to systemd services. If you edited it and the worker limit did not change, this is why. The service needs LimitNOFILE in a systemd drop-in.
- •A traffic spike or many idle keepalive connections. The errors appear only at busy times, and the descriptor count per worker climbs with connection count. Long keepalive_timeout values and slow clients hold descriptors open for longer.
- •A descriptor leak or very many open files. The count keeps rising even when traffic is flat. Run ls /proc/<worker pid>/fd | wc -l a few times and look at what the links point to. Large numbers of sockets stuck to a dead upstream, or a huge number of cache and log files, point here.
- •The system-wide limit is exhausted. This is a different error: (23: Too many open files in system), ENFILE. If you see 23 instead of 24, check fs.file-max, not the per-process limit.
How to fix it
1. Read the current limit of a running worker
This shows the limit nginx is really running under, which can differ from what your config files say. The soft limit (first number) is the one that matters.
for p in $(pgrep -f 'nginx: worker'); do echo "worker $p"; grep 'Max open files' /proc/$p/limits; done2. Count how many descriptors a worker uses now
Compare this number with the limit from step 1. If it sits close to the limit, you have confirmed the cause. Run it as root so it can read the /proc entries.
for p in $(pgrep -f 'nginx: worker'); do echo "worker $p: $(sudo ls /proc/$p/fd | wc -l) open"; done3. Set worker_rlimit_nofile and worker_connections in nginx.conf
Edit /etc/nginx/nginx.conf. Put worker_rlimit_nofile in the main context (top level, outside http and events). Keep it at least twice worker_connections, because proxied requests use two descriptors each. 65535 is a common starting value; size it to your traffic.
# /etc/nginx/nginx.conf worker_rlimit_nofile 65535; events { worker_connections 16384; }4. Raise LimitNOFILE for the nginx service in systemd
This works on Ubuntu, Debian and RHEL-family systems that use systemd. The command opens an editor for a drop-in file at /etc/systemd/system/nginx.service.d/override.conf. Add the two lines below, save, then reload systemd. systemd applies the new limit only when the service starts, so it takes effect after the restart in step 5. A reload of nginx does not change it.
sudo systemctl edit nginx # add these lines in the editor: [Service] LimitNOFILE=65535 sudo systemctl daemon-reload5. Test the config, then reload or restart nginx
Always test first. A reload keeps existing connections and is enough to apply worker_rlimit_nofile. A restart is needed to apply the new LimitNOFILE from step 4, and it briefly drops active connections, so do it in a quiet period or behind a load balancer.
sudo nginx -t && sudo systemctl reload nginx # only if you changed LimitNOFILE (drops active connections): sudo systemctl restart nginx6. Check the system-wide limits have room
The per-process limit cannot exceed fs.nr_open, and all processes together are capped by fs.file-max. The first number in file-nr is descriptors in use, and the last is the maximum. On most modern kernels these are already very high, so raise them only if the output shows a problem.
sysctl fs.file-max fs.nr_open cat /proc/sys/fs/file-nr7. If the count keeps climbing, look for the leak
Count connections by state and see where descriptors go. Many connections stuck in CLOSE_WAIT or a huge number to one upstream suggest a failing backend or a keepalive setting that is too generous. Consider lowering keepalive_timeout or fixing the slow upstream rather than only raising the limit.
sudo ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn sudo lsof -p $(pgrep -f 'nginx: worker' | head -1) | awk '{print $5}' | sort | uniq -c | sort -rn
Confirm it is fixed
Each worker should now report the higher limit, and no new accept4() lines should appear in the error log after the reload. Good output looks like 'Max open files 65535 65535 files' for every worker, and the grep for recent errors prints nothing.
for p in $(pgrep -f 'nginx: worker'); do grep 'Max open files' /proc/$p/limits; done
sudo tail -n 200 /var/log/nginx/error.log | grep 'Too many open files'How Opservo catches it early
The Opservo agent tails nginx logs, groups repeated lines into normalised patterns (raw log lines are never stored) and raises a recurring error finding with a plain-English AI summary when this error keeps coming back. It also flags open file descriptors above 80% of the system limit, so you can see the pressure building before workers run out.
Last updated 2026-11-30.