快速解决方案:
首先查看修改系统的最大打开文件限制 ,默认连接是: 1024 =》 ulimit -n ulimit -a 查看
如:ulimit -n 1000,则表示将当前shell的当前用户所有进程能打开的最大文件数量设置为1000 =》 临时性更改
linux最大文件句柄数量总结 值得看:linux最大文件句柄数量总结
1,位置: /proc/sys/fs/file-max
查看命令:cat /proc/sys/fs/file-max
修改:
通过vi修改 /etc/sysctl.conf 文件,在该文件中加上:
fs.file-max=2000000
sysctl -p 使修改立即生效,而无需重启; 可以通过下面命令验证是否成功 :cat /proc/sys/fs/file-max 或者:sysctl fs.file-max
2.修改nr_open
通过vi修改 /etc/sysctl.conf,在该文件中加上:
fs.nr_open = 2000000
可以在令针对此参数 的修改一直生效,在该文件中,修改完后执行命令:
sysctl -p 使修改立即生效,而无需重启
注意:
a. 所有进程打开的文件描述符数不能超过/proc/sys/fs/file-max
b. 单个进程打开的文件描述符数不能超过user limit中nofile的soft limit
c. nofile的soft limit不能超过其hard limit
d. nofile的hard limit不能超过/proc/sys/fs/nr_open
# vi /etc/sysctl.conf
fs.nr_open=1100000 //要比 hard nofile 大一点
fs.file-max=1100000 //多留点buffer
# sysctl -p
# vi /etc/security/limits.conf
* soft nofile 1000000
* hard nofile 1000000
基本命令:
# cat /etc/redhat-release
CentOS release 6.8 (Final)
# uname -r
2.6.32-642.el6.x86_64
查看系统级别的最大打开文件数可以使用 sysctl -a | grep fs.file-max 命令查看
top 命令详解:https://www.cnblogs.com/niuben/p/12017242.html
Linux查看机器负载 https://blog.csdn.net/szchtx/article/details/38455385
Linux最大打开文件描述符数 https://blog.csdn.net/SuperChanon/article/details/13303705
nginx 优化最大链接: https://www.jianshu.com/p/8f95bdaedd9e // https://www.cnblogs.com/liqiu/p/3152206.html
Syntax: worker_rlimit_nofile number;
Default: —
Context: main
为nginx工作进程改变打开最多文件描述符数目的限制。用来在不重启主进程的情况下增加限制。
例如: worker_rlimit_nofile 204800;
理论上这个值是最多打开文件数(ulimit -n)与nginx工作进程相除
访问Nginx时,报错:"accept() failed (24: Too many open files)" 原因时:nginx的连接数超过了系统设定的最大值造成的.
https://www.cnblogs.com/kevingrace/p/5815592.html 是否要加入: session required
/lib/security/pam_limits
.so
ulimit -a 详解:
https://www.xiaoz.me/archives/13011
二、Nginx配置限制
排除了系统的文件限制,可能就是Nginx自身配置导致的了。
# vim /etc/nginx/nginx.conf 这里为nginx配置文件
worker_rlimit_nofile 65535; //添加
events {
worker_connections 20480; //修改
}
Linux中soft nproc 、soft nofile和hard nproc以及hard nofile配置
"soft" 和 "hard" 的区别
soft xxx : 代表警告的设定,可以超过这个设定值,但是超过后会有警告。
hard xxx : 代表严格的设定,不允许超过这个设定的值。
修改系统的配置
打开/etc/security/limits.conf (部分的系统是在/etc/security/limits.d/90-nproc.conf)
设置限制数量,第一列表示用户,* 表示所有用户
soft nproc :单个用户可用的最大进程数量(超过会警告);
hard nproc:单个用户可用的最大进程数量(超过会报错);
soft nofile :可打开的文件描述符的最大数(超过会警告);
hard nofile :可打开的文件描述符的最大数(超过会报错);
重启后生效
reboot
注:①一般soft的值会比hard小,也可相等。
②/etc/security/limits.d/
里面配置会覆盖/etc/security/limits.conf
的配置
③只有root用户才有权限修改/etc/security/limits.conf
④如果limits.conf没有做设定,则默认值是1024