2025年3月14日 星期五 甲辰(龙)年 月十三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

centos8平台nginx服务配置打开文件限制max open files limits

时间:12-14来源:作者:点击数:13

一,nginx启动时提示打开文件数,ulimit的配置不起作用:

1,

  • 2020/04/26 14:27:46 [notice] 1553#1553: getrlimit(RLIMIT_NOFILE): 1024:4096

用户可打开文件数

软限制是:1024

硬限制是: 4096

这个值太小,不适用于服务器用途

2,用root用户查看ulimit -n

  • [root@blog 1554]# ulimit -n
  • 65535

检查limits.conf配置文件

  • [root@blog 1554]# cat /etc/security/limits.conf

limits.conf中有针对打开文件数的配置

  • * soft nofile 65535
  • * hard nofile 65535

可以看到不管ulimit命令还是配置文件,都做了打开文件数量的配置

3,确认当前进程是否有打开文件数量的限制?

用ps找到nginx的进程id:1554,查看master process进程的limits

  • [root@blog ~]# more /proc/1554/limits | grep 'open files'
  • Max open files 1024 4096 files

确实存在和ulimit中不一致的数量限制

为什么ulimit的配置不起作用?

可以访问这里: https://github.com/liuhongdi/

二,ulimit配置不起作用的原因:

因为ulimit和limits.conf的配置只针对登录用户,

而对nginx这个systemd管理的服务不起作用,

服务的limit要在service文件中单独指定

三,配置nginx打开文件数量:

 1,找到nginx的service文件

  • [root@blog 1554]# systemctl cat openresty.service
  • # /usr/lib/systemd/system/openresty.service
  • [Unit]
  • Description=The OpenResty Application Platform
  • After=syslog.target network.target remote-fs.target nss-lookup.target
  • [Service]
  • Type=forking
  • PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
  • ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
  • ExecStart=/usr/local/openresty/nginx/sbin/nginx
  • ExecReload=/bin/kill -s HUP $MAINPID
  • ExecStop=/bin/kill -s QUIT $MAINPID
  • PrivateTmp=true
  • [Install]
  • WantedBy=multi-user.target

2,编辑service文件,

  • [root@blog ~]# vi /usr/lib/systemd/system/openresty.service

在service段增加一行:

  • LimitNOFILE=65535

这样systemd就能了解到要对服务做的限制了

3,重启:

  • [root@blog ~]# systemctl daemon-reload
  • [root@blog ~]# systemctl stop openresty.service
  • [root@blog ~]# systemctl start openresty.service

4,查看日志看是否起作用:

  • [root@blog ~]# tail -100 /data/logs/nginxlogs/error.log
  • ...
  • 2020/04/26 14:36:22 [notice] 1651#1651: getrlimit(RLIMIT_NOFILE): 65535:65535
  • ...

说明已经起作用

5,可以查看进程中限制信息看是否起作用:

找到进程id

  • [root@blog ~]# ps auxfww | grep nginx
  • root 1652 0.0 0.0 50412 3348 ? Ss 14:36 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx
  • nginx 1653 0.0 0.0 81896 5908 ? S 14:36 0:00 \_ nginx: worker process
  • nginx 1654 0.0 0.0 81896 5908 ? S 14:36 0:00 \_ nginx: worker process
  • nginx 1655 0.0 0.0 81896 5908 ? S 14:36 0:00 \_ nginx: worker process
  • nginx 1656 0.0 0.0 81896 5908 ? S 14:36 0:00 \_ nginx: worker process

我们查看pid 1652的打开文件限制:

  • [root@blog ~]# more /proc/1652/limits | grep 'open files'
  • Max open files 65535 65535 files

可以看到修改已经生效

四,查看nginx的版本

  • [root@blog ~]# /usr/local/openresty/nginx/sbin/nginx -v
  • nginx version: openresty/1.15.8.2

五,查看centos的版本 

  • [root@blog ~]# cat /etc/redhat-release
  • CentOS Linux release 8.0.1905 (Core)
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐