开发者社区> zchd> 正文

Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误

简介: 我一个统计程序估计要跑1分多钟以上 查看了一个php-fpm 配置文件 [13-Oct-2013 12:06:07] WARNING: [pool www] child 7458, script '/home/wwwroot/admin/index.
+关注继续查看

我一个统计程序估计要跑1分多钟以上

查看了一个php-fpm 配置文件

[13-Oct-2013 12:06:07] WARNING: [pool www] child 7458, script '/home/wwwroot/admin/index.php' (request: "GET /index.php") execution timed out (101.515909 sec), terminating
[13-Oct-2013 12:06:07] WARNING: [pool www] child 7458 exited on signal 15 (SIGTERM) after 1130895.840878 seconds from start
[13-Oct-2013 12:06:07] NOTICE: [pool www] child 24885 started

很明显了

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

google了一较以后

http://rtcamp.com/wordpress-nginx/tutorials/php/increase-script-execution-time/ 

Changes in php.ini

If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.

vim /etc/php5/fpm/php.ini

Set…

max_execution_time = 300

In Apache, applications running PHP as a module above would have suffice. But in our case we need to make this change at 2 more places.

Changes in PHP-FPM

This is only needed if you have already un-commented request_terminate_timeout parameter before. It is commented by default, and takes value of max_execution_time found in php.ini

Edit…

vim /etc/php5/fpm/pool.d/www.conf

Set…

request_terminate_timeout = 300

Changes in Nginx Config

To increase the time limit for example.com by

vim /etc/nginx/sites-available/example.com
location ~ \.php$ {
	include /etc/nginx/fastcgi_params;
        fastcgi_pass  unix:/var/run/php5-fpm.sock;
	fastcgi_read_timeout 300; 
}

If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:

vim /etc/nginx/nginx.conf

Add following in http{..} section

http {
	#...
        fastcgi_read_timeout 300; 
	#...
}

Reload PHP-FPM & Nginx

Don’t forget to do this so that changes you have made will come into effect:

service php5-fpm reload
service nginx reload

原来,php-fpm有一个参数 max_requests,该参数指明了,每个children最多处理多少个请求后便会被关闭,默认的设置是500。因为php是把请求轮询给每个 children,在大流量下,每个childre到达max_requests所用的时间都差不多,这样就造成所有的children基本上在同一时间 被关闭。

在这期间,nginx无法将php文件转交给php-fpm处理,所以cpu会降至很低(不用处理php,更不用执行sql),而负载会升至很高(关闭和开启children、nginx等待php-fpm),网卡流量也降至很低(nginx无法生成数据传输给客户端)

解决问题很简单,增加children的数量,并且将 max_requests 设置未 0 或者一个比较大的值:

打开 /usr/local/php/etc/php-fpm.conf

然后重启php-fpm。

二、增加缓冲区容量大小

将nginx的error log打开,发现“pstream sent too big header while reading response header from upstream”这样的错误提示。查阅了一下资料,大意是nginx缓冲区有一个bug造成的,我们网站的页面消耗占用缓冲区可能过大。参考老外写的修 改办法增加了缓冲区容量大小设置,502问题彻底解决。后来系统管理员又对参数做了调整只保留了2个设置参数:client head buffer,fastcgi buffer size。

三、request_terminate_timeout

如果主要是在一些post或者数据库操作的时候出现502这种情况,而不是在静态页面操作中常见,那么可以查看一下php-fpm.conf设置中的一项:

request_terminate_timeout

这个值是max_execution_time,就是fast-cgi的执行脚本时间。

0s

0s为关闭,就是无限执行下去。(当时装的时候没仔细看就改了一个数字)

发现,问题解决了,执行很长时间也不会出错了。

优化fastcgi中,还可以改改这个值5s 看看效果。

php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉,都会出现502错误。

==============================================

我把以上的值300改成1000秒去了

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Visual Studio Code中的PHP提示错误:End of line character is invalid
Visual Studio Code中的PHP提示错误:End of line character is invalid
25 0
微服务没有gateway网关?那我就用nginx浅浅的反向代理一下吧
微服务没有gateway网关?那我就用nginx浅浅的反向代理一下吧
103 0
PHP解析json、xml错误
php内置函数json_decode() 可以解析json字符串 但是有的时候看起来正确的json,解析却一直返回null。 你知道吗,json是可能解析失败的,此时PHP不会产生提示。 我们需要手动通过json_last_error()函数获取
28 0
Linux 服务器 502 Bad Gateway nginx
前言,最近购买了一台阿里云服务器,尝试搭建自己的第一个博客网站,使用宝塔的工具以及使用java的命令进行部署的;
114 0
基于 nginx 部署 gateway 集群环境|学习笔记
快速学习基于 nginx 部署 gateway 集群环境
328 0
【微服务】一文读懂网关概念+Nginx正反向代理+负载均衡+Spring Cloud Gateway(多栗子)
不知道什么是网关?正向代理?反向代理?负载均衡?负载均衡策略?Nginx和Gateway的区别?假如这些你都不知道,没关系,本文举了大量通俗易懂的例子来阐述了这些概念,保证小白也能看懂,并且最后还提到了gateway的一些配置。
484 0
浏览器debug 调试一打开 Nginx 就 504 Gateway Time-out
浏览器debug 调试一打开 Nginx 就 504 Gateway Time-out
99 0
PHP错误与异常
PHP语言中错误与异常
25 0
+关注
zchd
架构,编程语言相关技术专家
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
《Nginx 代理系统常用手册》
立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手
立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册
立即下载