VeryHOST

VeryHOST知識庫-是專業的IDC百科知識平台

linux 并发问题accept() failed (24: Too many open files)

更新時間:2023-09-15

centos5.3 中 ulimit -n 为1024, 当Nginx连接数超过1024时,error.log中就出现以下错误:


[alert] 12766#0: accept() failed (24: Too many open files)


使用 ulimit -n 655350 可以把打开文件数设置足够大, 同时修改nginx.conf , 添加 worker_rlimit_nofile 655350; (与error_log同级别)


这样就可以解决Nginx连接过多的问题,Nginx就可以支持高并发。<还要修改nginx>


另外, ulimit -n 还会影响到mysql 的并发连接数。把他提高,也就提高了mysql并发。


注意: 用ulimit -n 2048 修改只对当前的shell有效,退出后失效。


修改方法


若要令修改ulimits的数值永久生效,则必须修改配置文档,可以给ulimit修改命令放入/etc/profile里面,这个方法实在是不方便,


还有一个方法是修改/etc/security/limits.conf


/etc/security/limits.conf 格式,文件里面有很详细的注释,比如


* soft nofile 655360


* hard nofile 655360


星号代表全局, soft为软件,hard为硬件,nofile为这里指可打开文件数。


把以上两行内容加到 limits.conf文件中即可。


另外,要使 limits.conf 文件配置生效,必须要确保 pam_limits.so 文件被加入到启动文件中。查看 /etc/pam.d/login 文件中有:


session required /lib/security/pam_limits.so

但是linux 64位 pam_limits.so 文件是在 /lib64/security/ 下面 一定要注意

修改完重新登录就可以见到效果,可以通过 ulimit -n 查看。

在/etc 下sysctl.conf 里增加 

fs.file-max = 655360查看最大文件cat /proc/sys/fs/file-max



nginx配置

worker_rlimit_nofile 655350;
events {
   use epoll;
   worker_connections 655350;
}
http {