Menu Close

How to solve “Too many open files” in nginx

Considering donating if you found my post helpful 😊

Buy Me a Coffee QR Code

I runs a busy website and had an issue with nginx not being able to handle the connection.

This is a snippet of the error:

2024/12/15 19:46:08 [alert] 34526#34526: *129729 socket() failed (24: Too many open files) while connecting to upstream

Solution:

The values being used are according to my preference. You can adjust them according to your requirements.

Step 1 – Edit sysctl.conf

$ sudo nano /etc/sysctl.conf

At the very bottom, paste this text and save

fs.file-max = 70000

Step 2 – Edit limits.conf:

Edit /etc/security/limits.conf

$ sudo nano /etc/security/limits.conf

Add text below at the very bottom

nginx       soft    nofile   10000
nginx       hard    nofile  30000

Step 3 – Edit nginx.conf:

$ sudo nano /etc/nginx/nginx.conf

Add worker_rlimit_nofile and edit worker_connections

worker_rlimit_nofile 30000;

events {
       worker_connections 4096;
       # multi_accept on;
}

Save the file.

Step 4 – Restart nginx:

$ sudo systemctl restart nginx

…and you are done.

Considering donating if you found my post helpful 😊

Buy Me a Coffee QR Code
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x