天天看點

記憶體不足導緻 nginx 崩潰的原因分析

閱讀本文大概需要 7 分鐘。

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

複制

4、 執行指令

sysctl -p

讓修改生效;

5、通過指令

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           

複制

6、 使用 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;
# 省略的内容           

複制

7、使用

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           

複制

8、上述示例的設定值均是對公共伺服器的配置,具體資料請根據系統實際需要進行設定;

9、如果上述方法仍然沒有解決問題,可以考慮:

1.使用服務的方式啟動 nginx 試試;

2.加配置記憶體。

參考文章:

http://www.cnblogs.com/sxlfybb/archive/2011/09/15/2177983.html