Relaxing Max Open Files for Specific Daemons with systemd
server
Published: 2016-04-02

This content has been moved from Qiita.

There are times when you want to relax the Max open files setting for services like nginx.

Prior to CentOS7, which did not use systemd, we had to modify limits.conf. However, with systemd, the process is simpler.

Let’s assume we’re having trouble with nginx.

Checking the Current Settings

# grep "^Max open files" /proc/`cat /var/run/nginx.pid`/limits
Max open files            1024                 4096                 files 

Currently, the hard limit is 4096, meaning that a single process cannot open more than 4096 files. This often becomes insufficient when nginx handles many files due to tuning worker_connections or caching.

Changing the Settings

# mkdir /etc/systemd/system/nginx.service.d
# vi /etc/systemd/system/nginx.service.d/limits.conf

[Service]
LimitNOFILE=65536

Applying the Changes

  • Reload the systemd configuration
# systemctl daemon-reload
  • Restart nginx to apply the new settings
# systemctl restart nginx                                                                                                   

Verifying the Changes

# grep "^Max open files" /proc/`cat /var/run/nginx.pid`/limits
Max open files            65536                65536                files

We have successfully relaxed the Max open files limit.

Note About Non-systemd Methods

The Max open files limit is only relaxed when using systemd. If you run nginx directly, for example with nginx -t, the limit will not be relaxed.

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [warn] 4096 worker_connections exceed open file resource limit: 1024
nginx: configuration file /etc/nginx/nginx.conf test is successful