The Too many open files error occurs when a process hits the limit on how many file descriptors (fds) it can open. Sockets, pipes, and regular files all consume fds.
Check the current limits
# soft/hard
ulimit -Sn # soft limit
ulimit -Hn # hard limit
# when
cat /proc/sys/fs/file-max
sysctl fs.file-max# fd
ls /proc/PID/fd | wc -l
# fd TOP 10
sudo lsof 2>/dev/null | awk '{print $2}' | sort | uniq -c | sort -rn | head -10Immediate fix — raise the limit for the current session
# (when when )
ulimit -n 65535
# check
ulimit -nPermanent fix 1 — /etc/security/limits.conf
sudo nano /etc/security/limits.conf#
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
# User when
deploy soft nofile 65535
deploy hard nofile 65535# PAM limits check
grep "pam_limits" /etc/pam.d/common-session
#
echo "session required pam_limits.so" | sudo tee -a /etc/pam.d/common-sessionYou must log out and reconnect for the change to take effect.
Permanent fix 2 — systemd services (recommended)
Services managed by systemd do not pick up limits.conf. Set the limit directly in the service file.
# override file
sudo systemctl edit nginx # or[Service]
LimitNOFILE=65535sudo systemctl daemon-reload
sudo systemctl restart nginx
# check
sudo cat /proc/$(pgrep nginx | head -1)/limits | grep "open files"Permanent fix 3 — system-wide kernel parameter
# when
sudo sysctl -w fs.file-max=2097152
#
echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.conf
sudo sysctl -pNginx-specific settings
Nginx can also be configured directly in nginx.conf.
# /etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 4096;
}sudo nginx -t && sudo systemctl reload nginxRoot cause — check for fd leaks
If errors continue after raising the limit, you may have an fd leak.
# fd list check
sudo lsof -p PID | head -50
# fd (10 )
watch -n 10 "ls /proc/PID/fd | wc -l"If the fd count keeps growing, the application is not closing files or sockets — a bug in the code.
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Questions & answers
Ask a question about this guide. We'll review and reply.