nginx open files limits 导致大量错误信息

Posted 醒袭z

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx open files limits 导致大量错误信息相关的知识,希望对你有一定的参考价值。

nginx  error.log 中出现大量如下错误信息:

[[email protected] nginx]# grep -aP ‘^20.* [crit]‘ error.log

2017/03/14 12:06:31 [crit] 3549#0: accept4() failed (24: Too many open files)

[[email protected] nginx]# grep -aP ‘^20.* [alert]‘ error.log

2017/03/14 16:04:27 [alert] 3551#0: *84168270 socket() failed (24: Too many open files) while connecting to upstream, client: 1.1.1.1, server:... 

由于系统limits  open files 限制导致以上错误,所以:

[email protected]1:~# cat /etc/security/limits.conf 
*   soft    nofile  655360
*    hard    nofile  655360
root    soft    nofile  655360
root    hard    nofile  655360
*   soft    core unlimited
*    hard    core unlimited
root    soft    core unlimited

[email protected]-mk-push-1:~# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 512240
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 655360
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) 512240
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

但以上只是系统的open files 限制设置为655350;而nginx的open files 是否继承系统open files 设置还需要重新启动nginx 进程。可以通过以下命令查看:

[email protected]1:~# cat /proc/`ps -ef | grep nginx|grep -v grep|head -1|awk {print $2}`/limits
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             512240               512240               processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       512240               512240               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us           

可以看到nginx进程的Max open files的Soft Limit和Hard Limit分别只有1024和4096,这是一个初始的值可以说是很低了,故之前出现的Too many open files就可以理解了。

总结:ulimit -a && cat /proc/`ps -ef | grep nginx|grep -v grep| head -1 | awk ‘{print $2}‘`/limits 必须保持一致,否则会导致大量连接失败,此时需要重启nginx 进程 。

由于我们nginx是通过supervisor纳管的,所以得重启supervisor这个服务才能使nginx的max open files与系统值保持一致。

以上是关于nginx open files limits 导致大量错误信息的主要内容,如果未能解决你的问题,请参考以下文章

centos8平台nginx服务配置打开文件限制max open files limits

nginx网页报 cannot open more files的处理方法

Too many open files的四种解决办法

too many files问题open_files_limit值更改无效(转)

解决ubuntu系统mysql open_files_limit配置不生效问题

mysql open_files_limit值更改无效