What the message looks like
accept4() failed (24: Too many open files)Common causes
- Per-process limit too low, often 1024 by default.
- Limit raised in
/etc/security/limits.conf– which does not apply to systemd services. - File descriptor leak: the application does not close files or connections.
- Many concurrent connections, e.g. WebSockets or slow upstreams.
- In nginx:
worker_connectionshigher than the workers' limit.
Check
- Show the actual limit of the running process
cat /proc/$(pgrep -o nginx)/limits | grep 'open files' - Count currently open descriptors
ls /proc/$(pgrep -o nginx)/fd | wc -l - Show the limit from the systemd unit
systemctl show nginx.service -p LimitNOFILE - Find processes with the most open files
lsof -n 2>/dev/null | awk '{print $1}' | sort | uniq -c | sort -rn | head
Fix
- Set the limit for the service:
systemctl edit nginxwith[Service]LimitNOFILE=65535, then restart. - For nginx also set
worker_rlimit_nofile 65535;. - If the number of open descriptors keeps rising, look for a leak instead of only raising the limit.
- Adjust
limits.conffor login shells, always systemd for services. - Check the system-wide limit:
cat /proc/sys/fs/file-nr.
Typical scenario
An nginx server with worker_connections 4096 produces errors at peak times, and the error log says 'accept4() failed (24: Too many open files)'. The limit was set to 65535 in /etc/security/limits.conf, with no effect. systemd services do not read that file. Check with cat /proc/$(pgrep -o nginx)/limits which limit the running process really has, and set it in the unit or with worker_rlimit_nofile.
FAQ
Why does ulimit -n show a high value but the service still fails?
ulimit shows your shell's limit. Services inherit limits from systemd. What counts is /proc/PID/limits of the running process.
How high can the limit be?
65535 is generous for most services. Higher is possible but hides leaks. Watch the number of open descriptors over time.
Why does limits.conf have no effect on services?
The file applies to login sessions through PAM. Services under systemd get their limits from the unit, with LimitNOFILE=, best set as an override via systemctl edit. Then restart the service and check the values in /proc/PID/limits.
Is a higher limit harmless?
Usually yes, an open descriptor costs little memory. If the number of open files grows steadily over time, though, that suggests a leak, such as connections that are never closed. ls /proc/PID/fd | wc -l over several minutes shows whether the value just fluctuates or keeps rising.
What is the difference to fs.file-max?
fs.file-max limits all open files across the whole system, while the per-process limit (nofile) limits each individual process. The process limit usually bites first. The system value only becomes relevant with very many parallel services.