通过systemd放宽特定守护进程的最大打开文件数
服务器
Published: 2016-04-02

这是从Qiita迁移过来的内容。

有时需要在nginx等环境中放宽最大打开文件数。

在CentOS7之前,由于没有systemd,需要修改limits.conf,而在systemd中可以更简单地处理这个问题。

我们假设以nginx为例。

现状确认

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

目前,硬限制的4096以上,单个进程无法打开的文件,而在进行worker_connections调优和缓存等操作时,nginx需要操作大量文件,这时常常会不够用。

修改设置

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

[Service]
LimitNOFILE=65536

使设置生效

  • 让systemd重新加载修正
# systemctl daemon-reload
  • 重启nginx并重启进程
# systemctl restart nginx                                                                                                   

变更后确认

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

成功放宽了最大打开文件数。

通过非systemd方式

由于只有通过systemd的方式才会放宽打开文件数,因此,如果直接执行nginx -t 等命令,
最大打开文件数将不会被放宽,请注意。

# 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