Increate max no of open files limit in Ubuntu 16.04/18.04 for Nginx

In Linux, you can change the maximum amount of open files. You may modify this number by using the ulimit command. It grants you the ability to control the resources available for the shell or process started by it.

In this short tutorial we will show you how to check your current limit of open files and files descriptions, but to do so, you will need to have root access to your system.

First, Lets see how we can find out the maximum number of opened file descriptors on your Linux system.

# maximum capability of system
[email protected]:~$ cat /proc/sys/fs/file-max
708444

# available limit
[email protected]:~$ ulimit -n
1024

# To increase the available limit to say 200000
[email protected]:~$ sudo vim /etc/sysctl.conf

# add the following line to it
fs.file-max = 200000

# run this to refresh with new config
[email protected]:~$ sudo sysctl -p

# edit the following file
[email protected]:~$ sudo vim /etc/security/limits.conf

# add following lines to it

* soft  nofile  200000   
* hard  nofile  200000
www-data  soft  nofile  200000
www-data  hard  nofile  200000
root soft nofile 200000   
root hard nofile 200000

# edit the following file
[email protected]:~$ sudo vim /etc/pam.d/common-session

# add this line to it
session required pam_limits.so

# logout and login and try the following command
[email protected]:~$ ulimit -n
200000

# now you can increase no.of.connections per Nginx worker
# in Nginx main config /etc/nginx/nginx.conf
worker_connections 200000;
worker_rlimit_nofile 200000;

 

Leave a Reply

Your email address will not be published. Required fields are marked *