NGINX is a popular web server used by many websites. Sometimes, you may get “Too Many Open Files” error in NGINX server. In this article, we will look at how to fix this problem.
How to Fix NGINX: Too Many Open Files Error
Here are the steps to fix NGINX “Too Many Open Files” error. It is important to understand that Linux sets soft/hard limits on the number of files NGINX is allowed to keep open at any given time. When this limit is exceeded, you see “Too Many Open Files” error.
You can view these limits using ulimit command
$ ulimit -Hn #hard limit $ ulimit -Sn #soft limit
There are a couple of ways to fix this problem. You can increase this limit in Linux at an OS level, or you can increase it at process level. We will look at both these approaches.
Also read : How to List NGINX Modules & Compiled Flags
Increase Open FD Limit at Linux OS Level
In this method, we basically increase the OS limits for NGINX to enable it to open more files. Open terminal and run the following command to open system configuration file.
$ sudo vi /etc/sysctl.conf
Look for the following line
fs.file-max = 70000
Increase 70000 to a number of your choice. Don’t increase it too much else it will cause memory-related problems. Save and close the file.
Open /etc/security/limits.conf in a text editor.
$ sudo /etc/security/limits.conf
Add the following lines to set soft and hard limits for NGINX.
nginx soft nofile 10000 nginx hard nofile 30000
Save and close the file. Reload the system configuration with the following commands.
$ sysctl -p
Also read : How to Redirect HTTP to HTTPS to Custom Port
Increase NGINX worker_rlimit_nofile Option
NGINX also provides worker_rlimit_nofile directive to control the number of open files. You can increase this limit to fix the “Too many open files” problem.
Open the file in a text editor.
$ vi /usr/local/nginx/conf/nginx.conf
Add or modify the following line to set open file limit to 25000
# set open fd limit to 25000 worker_rlimit_nofile 25000;
Save and close the file. Reload NGINX with updated configuration.
# /usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload
Now when you check the ulimits for NGINX, you will see updated values.
$ sudo ulimit -Hn 30000 $ sudo ulimit -Sn 10000
Please increase the NGINX file limits only if it is absolutely necessary. The default settings of Linux and NGINX are good enough for most websites. If you are not careful with these limits, it can affect performance of your website performance and even other processes in the system.
Also read : How to Rsync Files Between Two Servers
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.