Nginx

Nginx accept4() failed (24: Too many open files): causes and fix

Fix the nginx accept4() failed (24: Too many open files) error: check the worker limit, raise worker_rlimit_nofile and LimitNOFILE, and verify.

accept4() failed (24: Too many open files)

In short

Nginx logs accept4() failed (24: Too many open files) when a worker process tries to accept a new connection but has already used every file descriptor it is allowed. Error 24 is EMFILE, the per-process limit. Each client connection, upstream connection, log file and cached file uses one descriptor. New visitors get dropped or see errors until the limit is raised or the usage falls.

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. 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; done
  2. 2. 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"; done
  3. 3. 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. 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-reload
  5. 5. 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 nginx
  6. 6. 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-nr
  7. 7. 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.

FAQ

Related questions

What is the difference between error 24 and error 23?+

Error 24 (EMFILE) means one process hit its own descriptor limit, so raise worker_rlimit_nofile and LimitNOFILE. Error 23 (ENFILE) means the whole system ran out, so check fs.file-max and find which process is using too many.

Why did editing /etc/security/limits.conf not fix it?+

That file applies to login sessions through PAM. Nginx started by systemd does not read it, so set LimitNOFILE in a systemd drop-in and worker_rlimit_nofile in nginx.conf instead.

How high should I set worker_rlimit_nofile?+

Set it to at least twice worker_connections, since a proxied request holds one descriptor for the client and one for the upstream. Add headroom if you serve many static files or keep large caches. Then watch the real per-worker count and adjust.

Do I need to restart nginx or is a reload enough?+

A reload applies worker_rlimit_nofile to new workers. A change to LimitNOFILE in systemd needs a full restart of the service, because systemd sets that limit only when it starts the process.