经验之谈:nginx php 502 bad gateway 解决方法
2017-09-25 00:00
381 查看
今天在使用nginx时发现运行php页面会提示502 bad gateway这类错误了,下面我根据各位群友提供的一些方法完美的解决了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时间,例如:
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;
访问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; } |
附整理了一些其它可能导致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;
相关文章推荐
- 经验之谈:nginx php 502 bad gateway 解决方法
- nginx php 502 bad gateway一例解决方法
- nginx php-fpm unixsocket并发100以上报502解决方法
- unbuntu下 nginx php-fpm.sock 502 bad getaway权限问题,解决方法
- nginx加php-fpm出现502 bad gateway错误的5种解决方法
- lnmp架构nginx轮询php服务池解决经常502的方法
- nginx加php-fpm报502 bad gateway错误五种解决方法
- Nginx的500,502,504错误解决方法
- nginx调用php-fpm出错解决方法和nginx配置详解
- Nginx反向代理导致PHP获取不到正确的HTTP_HOST,SERVER_NAME,客户端IP的解决方法
- linux系统下nginx+php-fpm报错:502 Bad Gateway解决方法
- Nginx 502 Bad Gateway错误的原因分析与解决方法
- PHP(FastCGI)在Nginx的alias下出现404错误的解决方法
- NGINX 502 Bad Gateway 用upstream解决方法
- ubuntu+nginx 出现502 bad gateway解决方法
- 腾讯云服务器php+mysq+nginx配置出现的问题及解决方法(亲测)
- nginx报502 bad GateWay错误的解决方法
- 腾讯云服务器php+mysq+nginx配置出现的问题及解决方法(亲测)
- 深入探讨:Nginx 502 Bad Gateway错误的解决方法
- nginx不解析php代码;解决方法