If you have an issue with NGINX like the followings (the output of error_log file):
[crit] 25655#0: *524505514 open() "/usr/share/nginx/html/50x.html" failed (24: Too many open files)
[...]
[alert] 28720#0: *59757 socket() failed (24: Too many open files) while connecting to upstream...
Make sure to check the default value of NGINX, by running the following command line:
cat /etc/default/nginx
The default is set to a limit of 4096 files per (worker) process, which can be seen in /etc/default/nginx:
# Note: You may want to look at the following page before setting the ULIMIT.
# http://wiki.nginx.org/CoreModule#worker_rlimit_nofile
# Set the ulimit variable if you need defaults to change.
# Example: ULIMIT="-n 4096"
#ULIMIT="-n 4096"
Changing this file doesn't help. Instead, this needs to be set in /etc/security/limits.conf:
#@faculty hard nproc 50
#ftp hard nproc 0
#ftp - chroot /ftp
#@student - maxlogins 4
# Added Nginx limits
nginx soft nofile 30000
nginx hard nofile 50000
# End of file
Before the "End of file" line of the NGINX security limits file at: /etc/security/limits.conf
# Added Nginx limits
nginx soft nofile 30000
nginx hard nofile 50000
Once added, save the file with Ctrl + X and then Y to confirm the changes! Here a soft limit of 30k and a hard limit of 50k files are defined per the NGINX process. Additionally, the NGINX should be told how many files can be opened. In the main config file /etc/nginx/nginx.conf add:
worker_processes 4;
pid /run/nginx.pid;
# Increasing the limit of open files
worker_rlimit_nofile 30000;
Again, save the file. Now, after a service nginx restart the limits of the worker processes can be checked:
ps auxf | grep nginx
cat /proc/7028/limits | grep "open files"
The occurred problem should be solved!