How to Increase ulimit -n (Open File Limit) for Nginx on Linux

When running high-traffic web applications like Nginx, you may encounter errors related to the open file limit, such as:

too many open files
This means the system’s file descriptor limit is too low. Here’s how to increase the ulimit -n value, which defines how many open files a process can handle.

✅ Step 1: Check Which User Runs Nginx Workers

To apply the limit, you need to know which non-root user runs the nginx worker processes. Run the following command:

ps aux | grep nginx | grep -v root

This shows all nginx processes excluding root. You’ll typically see something like:

www 1234 0.0 0.1 123456 7890 ? Ss 10:00 0:00 nginx: worker process

In this case, the user is www.

Step 2: Edit Limits Configuration

Edit the /etc/security/limits.conf file using your favorite editor:

nano /etc/security/limits.conf

Add the following lines at the end of the file, replacing www with the actual nginx user if different:

www soft nofile 65535
www hard nofile 65535

  • soft nofile – The default open file limit

  • hard nofile – The maximum open file limit

Save and exit (Ctrl+O, then Ctrl+X in nano).

✅ Step 4: Reboot or Restart the Service

Reboot the server to apply limits globally:

reboot

Leave a Reply

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