Nginx: Too Many Open Files Error And Solution

Recently when I check on one of my website, which frequently getting the error 500 when number of user start to increase. Check out the Nginx log and found the following error:

2019/04/26 13:24:16 [crit] 21974#0: *3188937 open() “/usr/local/nginx/html/50x.html” failed (24: Too many open files), client: , request: “GET /file/images/background.jpg HTTP/1.1”, upstream: “http://10.8.4.227:81//file/images/background.jpg”, host: “example.com”

After spending some time to Google it and finally found out the root cause of the problem as well as the solution to solved this. So I just share it over here just in case anyone facing the same problem as I was.

Linux / UNIX sets soft and hard limit for the number of file handles and open files. You can use ulimit command to view those limitations:

su - nginx

To see the hard and soft values, issue the command as follows:

ulimit -Hn
ulimit -Sn

Increase Open FD Limit at Linux OS Level

Your operating system set limits on how many files can be opened by nginx server. You can easily fix this problem by setting or increasing system open file limits under Linux. Edit file /etc/sysctl.conf, enter:

# vi /etc/sysctl.conf

Append / modify the following line:

fs.file-max = 70000

Save and close the file. Edit /etc/security/limits.conf, enter:

# vi /etc/security/limits.conf

Set soft and hard limit for all users or nginx user as follows:

nginx       soft    nofile   10000
nginx       hard    nofile  30000

Save and close the file. Finally, reload the changes with sysctl command:

sysctl -p

nginx worker_rlimit_nofile Option (Increase Open FD Limit at Nginx Level)

Nginx also comes with worker_rlimit_nofile directive which allows to enlarge this limit if it’s not enough on fly at process level. To set the value for maximum file descriptors that can be opened by nginx process. Edit nginx.conf file, enter:

vi /usr/local/nginx/conf/nginx.conf

Append / edit as follows:

# set open fd limit to 30000
worker_rlimit_nofile 30000;

Save and close the file. Reload nginx web server, enter:

# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
# su - nginx
$ ulimit -Hn
$ ulimit -Sn

Sample outputs:

30000
10000


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: