Fix - Nginx socket() failed (24: Too many open files)

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!

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

How to restrict direct root access in Linux

We can do it just in two steps. Step One: At first we will create new root user as follows (for...

How to extract .tar.gz files in Linux/UNIX OS

A tarball is a group of files that are bundled together using the tar command. Use the...

How to add welcome message when SSH start?

You need to change the contents of /etc/motd. Unfortunately, by default, /etc/motd is a link to...

How to change root password when SSH logged in

Run the following command: passwd Now type your new passwordOnce done, retype new passwordDone!...

How to install Pinguzo on any Linux/UNIX OS

Login to Pinguzo panel using Softaculous account or create an account of Pinguzo To add new...