nginx "Too many open files" (24) error: how to fix it

nginx logs accept4() failed (24: Too many open files) when it hits its file descriptor limit. How to check the real limit, raise it with systemd and nginx, and avoid it.

· 6 min read · By the Spot Downtime team

Your site slows down, connections fail or visitors get resets under load, and the nginx error log contains lines like these:

text
[crit] 1234#1234: accept4() failed (24: Too many open files)
[alert] 1234#1234: 1024 worker_connections are not enough
[crit] *5678 open() "/var/www/site/index.html" failed (24: Too many open files)

Errno 24 is EMFILE: the process reached its limit of open file descriptors. In nginx, every client connection, every upstream connection and every open file uses a descriptor, so a busy server can run out faster than expected.

Why nginx runs out

A descriptor is a handle to a file or socket. Linux caps how many one process may hold (the soft limit, often just 1024 by default). nginx needs roughly:

  • One descriptor per client connection.
  • One more per proxied request to an upstream (so a reverse proxy uses about two per active request).
  • Descriptors for static files being served, log files and cache files.

With a limit of 1024 and a reverse-proxy setup, you can hit the ceiling at around 500 simultaneous requests per worker.

Step 1: check the limit nginx actually has

Do not trust ulimit -n in your own shell. Check the running worker processes, because that is what counts:

bash
# find a worker pid
ps -o pid,cmd -C nginx

# the limits that process really has
cat /proc/<worker-pid>/limits | grep "open files"

# how many it is using right now
ls /proc/<worker-pid>/fd | wc -l

If the second number is close to the soft limit in the first, you have found the cause. If the limit shows 1024 or 4096 and your traffic is high, raise it.

Step 2: raise the limit for nginx

Two settings cooperate: the operating system limit for the service, and nginx's own request to raise it. Do both.

systemd services

On modern distributions nginx runs under systemd, which ignores /etc/security/limits.conf for services. Set the limit in a drop-in:

bash
sudo systemctl edit nginx

# in the editor that opens:
[Service]
LimitNOFILE=65535

sudo systemctl daemon-reload
sudo systemctl restart nginx

A plain reload does not apply a new systemd limit, because the master process keeps its old one. Restart it.

nginx configuration

In nginx.conf, ask nginx to raise the limit for its workers and size connections to match:

nginx
# main context (top of nginx.conf)
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 16384;
}

worker_rlimit_nofile sets the descriptor limit for worker processes. The theoretical maximum of simultaneous client connections is worker_processes × worker_connections, but as a reverse proxy count two descriptors per request, so keep worker_rlimit_nofile at least double worker_connections. Validate and reload:

bash
sudo nginx -t && sudo systemctl reload nginx

The “worker_connections are not enough” alert is the sibling problem: raise worker_connections as shown, and check it did not exceed the descriptor limit.

Containers and other launchers

  • Docker: pass --ulimit nofile=65535:65535 to docker run, or set ulimits in your Compose file.
  • init scripts without systemd: run ulimit -n 65535 in the script before starting nginx.
  • System-wide ceiling: cat /proc/sys/fs/file-max shows the kernel maximum. It is normally far larger than you need, but check it on small or unusual hosts.

If raising the limit does not fix it

A higher limit only helps if you are legitimately busy. If the descriptor count keeps climbing toward any limit, look for a leak or a stuck dependency:

  • Slow upstreams. Requests waiting on a slow application hold two descriptors each. A stalled backend makes descriptors pile up. See upstream timed out.
  • Long keep-alive timeouts. Thousands of idle connections each hold one. Lower keepalive_timeout if idle clients dominate.
  • Slow or abusive clients. Slowloris-style traffic holds connections open. Use limit_conn and sensible client_header_timeout and client_body_timeout values.
  • Huge logging or caching setups. Dozens of log files or a big open_file_cache count against the limit.
bash
# connections by state, to spot piles of idle or waiting sockets
ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn

How to see it before visitors do

Descriptor exhaustion usually arrives as intermittent failures at peak hours, not a full outage, so a single check against your homepage at noon may be fine. Watching for it means checking often and from outside, and keeping an eye on the server itself. A website monitor catches the failed or slow responses, and server monitoring shows the host resources beside them.

Quick checklist

Read /proc/<pid>/limits for the worker. Set LimitNOFILE in a systemd drop-in and worker_rlimit_nofile in nginx. Restart, not reload. Then check whether descriptors still grow: that points at slow upstreams or idle connections.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading