加V:19841731,领 MTSC 大会历届 PPT

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

上一篇 / 下一篇  2019-06-12 12:03:49 / 个人分类:Python

最近在 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
  3. 使用 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
  3. 使用 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;
    # 省略的内容
  4. 使用reboot命令重启系统后,我们分别使用ulimit -Hnulimit -Snulimit -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
  5. 上述示例的设置值均是对公共服务器的配置,具体数据请根据系统实际需要进行设定;

  6. 如果上述方法仍然没有解决问题,可以考虑:

    1.使用服务的方式启动 nginx 试试;
    2.加配置内存。

参考文章
http://www.cnblogs.com/sxlfybb/archive/2011/09/15/2177983.html


TAG:

 

评分:0

我来说两句

sylan215

sylan215

加V「19841731」,领 MTSC 大会历届 PPT。

日历

« 2024-04-18  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 110158
  • 日志数: 91
  • 图片数: 1
  • 建立时间: 2018-07-03
  • 更新时间: 2021-11-11

RSS订阅

Open Toolbar