Too many open files error in nginx

If you receive below error in nginx log it means that the default limit for the nginx process was reached:

failed (24: Too many open files)

Upon checking we can see that the process is limited to 1024 open files (soft limit)

[root@~]# ps aux | grep nginx
root 15561 0.0 0.0 59128 1364 ? Ss 11:30 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 15562 2.7 0.0 61140 5012 ? S 11:30 0:00 nginx: worker process
root 15571 0.0 0.0 112708 944 pts/2 S+ 11:30 0:00 grep --color=auto nginx

[root@~]# cat /proc/15561/limits |grep open
Max open files 1024 4096 files



The fix is to increase the limit in /lib/systemd/system/nginx.service file:

Add LimitNOFILE=100000 under [Service] tag:

The file should look like this when you are done:

 [Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target


then issue a daemon reload:

systemctl daemon-reload

and 

service nginx restart


check your new setting:


ps aux |grep nginx
root 16589 0.0 0.0 59128 1360 ? Ss 11:35 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
cat /proc/16589/limits |grep open
Max open files 100000 100000 files

  • Too many open files, nginx error
  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

 Gzip compression enable on nginx

Working configuration for gzip and allowing some extensions to be compressed: gzip...

 Nginx Proxy timeout backend

If you have an nginx server that communicates with a server in the backend and it times out after...

 Wordpress nginx rule to replace htaccess

As some of you may know, nginx does not work with mod rewrite, and therefor it does not work with...