经验之谈:nginx php 502 bad gateway 解决方法

访问phpMyAdmin时,出现下面错误。

phpMyAdmin – Error

Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.第一次打开提示,刷新提示:

502 bad gateway

查看nginx error log:

[error]  recv() failed (104: Connection reset by peer) while reading response header from upstream, client:, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "mysql.veryi.com"

查看php session.save_path的设置,默认是/var/lib/php/session。修改该目录nginx进程的用户有读写权限。问题解决。

其他可能:

部分PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间,例如:

 代码如下 复制代码
http
{
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}

php.ini中memory_limit设低了会出错,可以加大php.ini的memory_limit为64M。

附整理了一些其它可能导致502 bad gateway错误原因

.php-fpm进程数不够用

使用 netstat -napo |grep "php-fpm" | wc -l 查看一下当前fastcgi进程个数,如果个数接近conf里配置的上限,就需要调高进程数。

但也不能无休止调高,可以根据服务器内存情况,可以把php-fpm子进程数调到100或以上,在4G内存的服务器上200就可以。

2. 调高调高linux内核打开文件数量

可以使用这些命令(必须是root帐号)

echo 'ulimit -HSn 65536' >> /etc/profile

echo 'ulimit -HSn 65536' >> /etc/rc.local

source /etc/profile

 3.脚本执行时间超时

如果脚本因为某种原因长时间等待不返回 ,导致新来的请求不能得到处理,可以适当调小如下配置。

nginx.conf里面主要是如下

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

php-fpm.conf里如要是如下

request_terminate_timeout = 10s

4.缓存设置比较小

修改或增加配置到nginx.conf

proxy_buffer_size 64k;
proxy_buffers  512k;
proxy_busy_buffers_size 128k;

5. recv() failed (104: Connection reset by peer) while reading response header from upstream

可能的原因机房网络丢包或者机房有硬件防火墙禁止访问该域名

但最重要的是程序里要设置好超时,不要使用php-fpm的request_terminate_timeout,

最好设成request_terminate_timeout=0;

时间: 2024-09-21 08:45:30

经验之谈:nginx php 502 bad gateway 解决方法的相关文章

nginx提示502 页面的解决方法_nginx

例如: 复制代码 代码如下:     http      {      ......      fastcgi_connect_timeout 300;      fastcgi_send_timeout 300;      fastcgi_read_timeout 300;      ......      }    也有可能是PHP代码不正确,比如SVN冲突等原因.

nginx安装wordpress 502 Bad Gateway解决办法

wordpress 502 Bad Gateway解决办法 首先是更改php-fpm的几处配置: /www/wdlinux/php/etc/php-fpm.conf /www/wdlinux/etc/php-fpm.conf 把max_children由之前的10改为现在的30,这样就可以保证 有充足的php-cgi进程可以被使用: 把request_terminate_timeout由之前的0s改为60s,这样php-cgi进程 处理脚本的超时时间就是60秒,可以防止进程都被挂起,提高利用效率

Nginx提示502 Bad Gateway错误问题解决方法小结

502 Bad Gateway错误问题解决方法 Nginx 502错误的原因比较多,是因为在代理模式下后端服务器出现问题引起的.这些错误一般都不是nginx本身的问题,一定要从后端找原因!但nginx把这些出错都揽在自己身上了,着实让nginx的推广者备受置疑,毕竟从字眼上理解,bad gateway?不就是bad nginx吗?让不了解的人看到,会直接把责任推在nginx身上,希望nginx下一个版本会把出错提示写稍微友好一些,至少不会是现在简单的一句 502 Bad Gateway,另外还不

Nginx中错误提示:502 Bad Gateway解决办法

php-fpm支持的php-cgi 修改php-fpm 进程数在/usr/local/php/etc/php-fpm.conf 查找max_children 将后面的值修改成你要设置的值就可以 .  代码如下 复制代码 #vim  /usr/local/php/etc/php-fpm.conf <value name="max_children">4</value>        #max_children表示php-cgi的处理进程,设置max_childre

解决php导致nginx报502 bad gateway错误问题

502 bad gateway不一定全是nginx,apache之类引起的,也有可能是由其他模块引起的,例如:php 1,查看php-fpm和nginx的log # vim /var/log/nginx/error.log recv() failed (104: Connection reset by peer) while reading response header from upstream # vim /var/log/php-fpm/error.log WARNING: [pool

nginx在post大量字段时出现 502 Bad Gateway解决办法

今天遇到一个奇怪的问题,在提交表单时直接就出现 502 Bad Gateway! 开始怀疑是程序问题,后来检查程序正常,发现post字段数大于1000时就出现502 Bad Gateway 而不超过1000时就正常,Google了下.发现有提到 php.ini里配置项 max_input_vars 默认没有开启,默认值是1000 于是修改 max_input_vars为自己适合的值. 修改后重启php service php-fpm stop service php-fpm start 再次提交

Nginx启动常见错误及解决方法_nginx

重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)错误,进到logs文件发现的确没有nginx.pid文件 [root@localhost sbin]# ./nginx -s reload nginx: [error]

Nginx 遇到502 Bad Gateway 自动重启的脚本代码_nginx

原理就是用curl获取HTTP头,发现502状态码就执行重启php-fpm的命令. 复制代码 代码如下: #!/usr/bin/php <!--$url = 'http://www.jb51.net'; $cmd = '/usr/local/php/sbin/php-fpm restart'; for($i = 0; $i < 5; $i ++){ $exec = "curl connect-timeout 3 -I $url 2>/dev/null"; $res =

nginx php 502 Bad Gateway错误原因分析

发生原因 1.PHP FastCGI进程数不够用 当网站并发访问巨大时,php fastcgi的进程数不有一定的保障,因为cgi是单线程多进程工作的,也就是说cgi需要处理完一个页面后再继续下一个页面.如果进程数不够,当访问巨大的时候,cgi按排队处理之前的请求,之后的请求只有被放弃.这个时候nginx就会不时的出现502错误. 2.PHP FastCGI的内存不够用 当nginx返回静态页面时,这个问题一般不会出现,因为nginx不需要php cgi的处理而直接返回静态页面.但是当网页需要处理