一、问题
nginx连接数暴涨,查看error.log文件,发现很多
failed (24: Too many open files)
二、原因
Linux是限制系统/应用/用户打开文件的数量的,当应用已经打开了上限数量的文件,再有打开新的文件的请求,之前打开的文件又没有关闭,就会报错,请求失败。
查看当前所有的打开文件数和某应用的打开文件数
lsof |wc -l
lsof -ap [PID]|wc -l
三、解决方法
1、 修改系统打开文件的数量上限
打开文件
vim /etc/sysctl.conf
fs.file-max = 1048576
使配置生效
sysctl -p
2、 修改nginx配置
Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.
修改工作进程打开文件的最大数量限制。无需重启主进程,即可提高限制数。
这里我设置为worker_connections值的10倍,假定每个连接可以打开十个文件吧,不知对否。
重启nginx即可
nginx -s reload