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