当前位置: 首页 >> Nginx >> 【报错】Nginx报错: "Too many open files accept" 和 "could not build the server_names_hash" >> 正文

【报错】Nginx报错: "Too many open files accept" 和 "could not build the server_names_hash"

5年前 (2019-05-27)     作者:Jiaozn     分类:Nginx     阅读次数:2422     评论(0)    

一、访问Nginx时,报错:"accept() failed (24: Too many open files)"

原因时:nginx的连接数超过了系统设定的最大值造成的. 

处理办法如下: 


[root@kvm-server nginx]# ulimit -n

1024

[root@kvm-server nginx]# ulimit -n 655360       #把打开文件数设置足够大,这是临时修改方案

[root@kvm-server nginx]# ulimit -n

655360

 

同时修改nginx.conf文件,添加下面内容,然后重启nginx

worker_rlimit_nofile 655350;

 

这样就可以解决Nginx连接过多的问题,Nginx就可以支持高并发。

另外, ulimit -n 还会影响到mysql 的并发连接数。提高文件连接数设置,也能提高mysql并发。

 

注意: 用ulimit -n 655360 修改只对当前的shell有效,退出后失效。所以,需要永久性修改

永久生效方法:

修改/etc/security/limits.conf,在文件底部添加:

* soft nofile 655360

* hard nofile 655360

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

 

另外,要使limits.conf文件配置生效,必须要确保 pam_limits.so 文件被加入到启动文件中。

查看 /etc/pam.d/login 文件中有:

session required /lib/security/pam_limits.so

 

这样,问题就迎刃而解了!

ulimit : 设置最大进程数和最大文件打开数, 这个一般是系统优化的必要手段.


1) 临时修改

为了优化linux性能,可能需要修改这个最大值。临时修改的话ulimit -n 655360就可以了,重启后失效。

[root@localhost ~]# ulimit -n

1024

[root@localhost ~]# ulimit -n 655360

[root@localhost ~]# ulimit -n

655360

 

2) 永久修改

修改/etc/security/limits.conf文件, 在文件末尾添加

[root@localhost ~]# vim /etc/security/limits.conf

 

* soft nofile 655360

* hard nofile 655360

* soft nproc 655360

* hard nproc 655360

 

=============================

上面配置内容中:

*               代表针对所有用户

noproc     是代表最大进程数

nofile      是代表最大文件打开数

 

如上修改后重启服务或服务器,如果发现没更改过来, 还需要修改下面梁文文件

 

在/etc/security/limits.d/90-nproc.conf文件末尾添加

[root@localhost ~]# vim /etc/security/limits.d/90-nproc.conf

* soft nproc 655360

* hard nproc 655360

 

在/etc/security/limits.d/def.conf文件末尾添加

[root@localhost ~]# vim /etc/security/limits.d/def.conf

* soft nofile 655360

* hard nofile 655360

 

然后重启后生效

                                                                                                                                                               


二、重启Nginx时, 出现报错提示: "could not build the server_names_hash, you should increase server_names_hash_bucket_size: 64"


解释说明: 保存服务器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果 hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键值。因此,如果Nginx给出需要增大 hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.


解决办法:在nginx配置文件nginx.conf里的http{}段增加一行配置"server_names_hash_bucket_size 64;" ,如果64还不够,那么就按32的倍数往上加,比如128或256或512。


除非注明,发表在“Jiaozn的博客”的文章『【报错】Nginx报错: "Too many open files accept" 和 "could not build the server_names_hash"』版权归Jiaozn所有。 转载请注明出处为“本文转载于『Jiaozn的博客』原地址https://www.jiaozn.com/reed/455.html
0

评论

发表评论   

昵称*

E-mail*(建议输入,以便收到博主回复的提示邮件)

网站

分享:

支付宝

微信