最近在 Centos7 上搭建 nginx 作为 web 服务器使用,但是使用过程中,nginx 总是莫名其妙的崩掉,使用命令
dmesg
检查错误信息如下:
[6655217.659132] Out of memory: Kill process 11494 (lsof) score 10 or sacrifice child
[6655217.659567] Killed process 11494 (lsof) total-vm:161160kB, anon-rss:42368kB, file-rss:0kB, shmem-rss:0kB
2017/10/26 22:59:45 [crit] 13093#0: accept4() failed (23: Too many open files in system)
2017/10/26 22:59:45 [crit] 13092#0: accept4() failed (23: Too many open files in system)
经过高人指点,是系统配置设置没法满足当前的使用量,准确点说是系统的 open files (打开文件数目)配置的太低了。
使用命令
ulimit -a
看一下当前置:
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15089
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 15089
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
可以看到 open files 值,只有 1024,下面我们就详细说一下如何在 Centos 系统级别提高打开文件数目(open files)的限制。
[root@test /]# cat /etc/sysctl.conf
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.tcp_challenge_ack_limit = 999999999
kernel.kptr_restrict = 1
fs.file-max = 100000