NGINX出现500错误的常见解决,提高linux系统的链接数限制到51200

nginx出现500错误,不会显示具体错误信息。要到日志文件里面去看,通过yum安装的nginx的日志文件位于/var/log/nginx/error.log

日志描述

日志文件里面出现类似(24: Too many open files)的错误信息,这是说明nginx太过于繁忙,打开文件的数量超过linux的系统限制。

解决办法

需要对3个文件进行修改,分别是:

一、/etc/security/limits.conf

使用vi打开文件,然后在文件里增加以下代码

soft nofile 51200
hard nofile 65535

二、修改/etc/pam.d/login,在文件中增加

session required /lib/security/pam_limits.so
session required /lib64/security/pam_limits.so #64位系统请使用这个

三、nginx.conf

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
 
events
{
  use epoll;
  worker_connections 51200;
}

然后重启服务器 ulimit -n 查看文件限制,应该显示51200

参考资料

《搭建胜过Apache十倍的Web服务器(第4版)》