Nginx频繁报“500 Internal Server Error”错误

服务器导致访问量激增,频繁报“500 Internal Server Error”错误。我查了一下nginx的错误日志(apt-get方式安装的nginx的错误日志在/var/log/nginx/error.log),发现了大量的“[alert] xxxxx#0: accept() failed (24: Too many open files)”
11655386 socket() failed (24: Too many open files) while connecting to upstream,
访问量高时,由于系统对于进程的最大文件打开数的限制(ulimit -n 默认65535),而nginx属于单进程多线程并发的服务,所以在访问量高时,连接数超过65535后,会被系统限制连接。

我上网查了一下,说这是超过了最大打开文件数的限制。
还有现在worker_process 还是4 最好调成8 和cpu核数一样 这样才能充分用到多核
然后最好也顺便把net.ipv4.ip_local_port_range 调成1024-65000 这样当负载真上去了 nginx自己的本地端口不会用完

解决方法是:

查看最大打开文件数
ulimit -a
ulimit -HSn 102400 #只是当前shell有效

1 打开/etc/security/limits.conf文件,加上两句 
* soft nofile 655350
* hard nofile 655350
2 打开/etc/nginx/nginx.conf 
在worker_processes的下面增加一行 
worker_rlimit_nofile 65535; 
3 重新启动nginx,重新载入设置 

本文来自博客园,作者:武兴旺,转载请注明原文链接:https://www.cnblogs.com/wuxingwang/p/14297578.html

原文地址:https://www.cnblogs.com/wuxingwang/p/14297578.html