一,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://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,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)