首发于 sylan215
内存不足导致 nginx 崩溃的原因分析

内存不足导致 nginx 崩溃的原因分析

最近在 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


使用命令 cat /var/log/nginx/error.log 来查看 nginx 的错误日志包含如下信息:

  • 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)的限制。

详细步骤:

  1. 使用命令 sudo bash 切换到 root 账户;
  2. 使用 vi/vim 编辑 /etc/sysctl.conf 增加一行 fs.file-max = 100000 ,下面是修改后的结果:
  • [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


  1. 使用 vi/vim 编辑 /etc/security/limits.conf ,并在末尾增加如下语句,用来增加所有用户的软硬句柄和文件打开数目限制:
  • * soft nofile 100000
  • * hard nofile 300000


下面是修改后的结果:

  • [root@test /] # cat /etc/security/limits.conf
  • # 省略的内容
  • # End of file
  • * soft nofile 100000
  • * hard nofile 300000


  1. 执行命令 sysctl -p 让修改生效;
  2. 通过命令 whereis nginx 查看 nginx 配置文件所在位置:
  • [root@test /] # whereis nginx
  • nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz


  1. 使用 vi/vim 编辑 /etc/nginx/nginx.conf 来在 nginx 级别上提高打开的文件句柄限制:
  • [root@test /] # cat /etc/nginx/nginx.conf
  • # 省略的内容
  • user nginx;
  • worker_processes auto;
  • error_log /var/log/nginx/error.log;
  • pid /run/nginx.pid;
  • worker_rlimit_nofile 300000;
  • # 省略的内容


  1. 使用 reboot 命令重启系统后,我们分别使用 ulimit -Hn ulimit -Sn ulimit -a 来查看修改后的效果:
  • [root@test /] # ulimit -Hn
  • 300000
  • [root@test /] # ulimit -Sn
  • 100000
  • [root@test /] # 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) 100000
  • 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


  1. 上述示例的设置值均是对公共服务器的配置,具体数据请根据系统实际需要进行设定;
  2. 如果上述方法仍然没有解决问题,可以考虑:
1.使用服务的方式启动 nginx 试试;
2.加配置内存。

参考文章:
cnblogs.com/sxlfybb/arc

发布于 2019-07-30 21:35

文章被以下专栏收录