Home> php教程> PHP开发> body text

nginx prompt: 500 Internal Server Error solution

高洛峰
Release: 2017-01-06 16:27:10
Original
2452 people have browsed it

现在越来越多的站点开始用 Nginx ,("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。

在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据测试结果,Nginx 0.6.31 + PHP 5.2.6 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。

但很多人用 Nginx 的时候都会出现 500 错误,根据我使用的情况来看,很大一部分原因是 因为文件打开句柄太小有关。

在linux 下 使用这个命令增加进程打开的文件句柄。
ulimit -SHn 51200
默认只用1000 当链接数小的时候看不出来,使用这种处理方法可以有效防止500错误出现。
今天访问网站的时候,偶尔会遇上500 Internal Server Error的错误提示页面.
查了相关资料认为是访问过大,系统内核进程受限才出现的.

答案如下:

$ ulimit -n
11095

程序限制只能打开11095个文件,ulimit命令是设置当前用户一个进程可拥有的文件描述符的数量.
看来是模拟的并发数太多了,需要调整一下nginx.conf的并发设置数,(我的配置主机的内存2G,CPU为2.8G,)

vi /etc/nginx/nginx.conf events { worker_connections 1024; }
Copy after login

调整为

events { worker_connections 10240; }
Copy after login

还是会出现上面问题,使用
[root@qimutian nginx]# cat /proc/sys/fs/file-max
8192
文件系统最大可打开文件数
[root@qimutian nginx]# ulimit -n
1024
程序限制只能打开1024个文件
使用[root@qimutian nginx]# ulimit -n 8192调整一下
或者永久调整打开文件数 可在启动文件/etc/rc.d/rc.local末尾添加(在/etc/sysctl.conf末尾添加fs.file-max=8192)
ulimit -n 8192
调整CentOS5文件打开数
使用ulimit -a一下,发现OPEN FILES不能默认超过1024,昨天的在进行压力测试时,出现500错误,具体请查看
nginx出现 500 Internal Server Error
早上起来看一下,发现原来是通过如下方式调整
方法1 (永久调整)

vi /etc/security/limits.conf

在文件末加上:

* soft nofile 8192
* hard nofile 20480

同时vi /etc/sysctl.conf末尾添加
fs.file-max=8192
重新启动,在使用ulimit -n查看的数已经是8192

方法2 (临时用)

直接在终端输入 ulimit -n 8192 按回车就ok了

500 Internal Server Error错误补充:

1、硬盘空间满了

使用 df -k 查看硬盘空间是否满了。清理硬盘空间就可以解决500错误。nginx如果开启了access log,在不需要的情况下,最好关闭access log。access log会占用大量硬盘空间。

2、nginx配置文件错误

这里不是指语法错误,nginx如果配置文件有语法错误,启动的时候就会提示。当配置rewrite的时候,有些规则处理不当会出现500错误,请仔细检查自己的rewrite规则。如果配置文件里有些变量设置不当,也会出现500错误,比如引用了一个没有值的变量。

3、如果上面的问题都不存在可能是模拟的并发数太多了,需要调整一下nginx.conf的并发设置数

解决方法是:

1 打开/etc/security/limits.conf文件,加上两句

* soft nofile 65535 * hard nofile 65535
Copy after login

2 打开/etc/nginx/nginx.conf
在worker_processes的下面增加一行

worker_rlimit_nofile 65535;
Copy after login

3 重新启动nginx,重新载入设置

kill -9 `ps -ef | grep php | grep -v grep | awk '{print $2}'` /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 100 -u www-data -f /usr/bin/php-cgi killall -HUP nginx
Copy after login

重启后再看nginx的错误日志,也没有发现500报错的情况了。

4、有可能是数据库问题我的在nginx日志php日志都没有发现什么问题, 最后发现数据库访问不了,修正后问题解决.

更多nginx提示:500 Internal Server Error错误的解决方法相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!