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秒去了

目录
相关文章
|
3月前
|
前端开发 应用服务中间件 API
比Nginx更好用的Gateway!
比Nginx更好用的Gateway!
55 1
比Nginx更好用的Gateway!
|
7月前
|
开发框架 应用服务中间件 PHP
Mac Nginx 关联 php 详细配置以及常见错误
Mac Nginx 关联 php 详细配置以及常见错误
141 1
|
22天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
18 0
|
4月前
|
应用服务中间件 nginx
nginx 504 Gateway Time-out
nginx 504 Gateway Time-out
103 0
|
6月前
|
缓存 JavaScript 应用服务中间件
Spartacus 部署到 CCV2 上造成 Nginx 返回 504 Gateway Timeout 错误的一个原因
Spartacus 部署到 CCV2 上造成 Nginx 返回 504 Gateway Timeout 错误的一个原因
39 0
|
4月前
|
PHP 调度
PHP CGI、FastCGI、PHP-FPM、PHP-CGI 区别
PHP CGI、FastCGI、PHP-FPM、PHP-CGI 区别
28 5
|
4月前
|
NoSQL 关系型数据库 应用服务中间件
Linux安装 OpenResty、Nginx、PHP、Mysql、Redis、Lua、Node、Golang、MongoDB、Kafka等
Linux安装 OpenResty、Nginx、PHP、Mysql、Redis、Lua、Node、Golang、MongoDB、Kafka等
88 0
|
4月前
|
负载均衡 Java 应用服务中间件
一文速通Nginx网关与gateway网关区分
一文速通Nginx网关与gateway网关区分
|
4月前
|
应用服务中间件 Linux PHP
百度搜索:蓝易云【在centos中配置nginx+php的环境教程。】
请注意,上述步骤是基本的配置指南,实际操作中可能会有特定的需求和配置。建议参考Nginx和PHP官方文档或其他详细教程以获得更全面和准确的信息。
45 0
|
4月前
|
Ubuntu 网络协议 应用服务中间件
Ubuntu安装PHP和NGINX环境
PHP-FPM 是 PHP FastCGI Process Manager 的缩写,是 FastCGI 进程管理器。 PHP-FPM 是基于 master/worker 的多进程架构模式,与 nginx 的设计风格类似。master 进程主要负责 CGI、PHP 环境初始化,事件监听、子进程状态,worker 进程负责处理 PHP 请求。 FPM 的 master 通过共享内存获取 worker 进程的信息,包括 worker 进程当前状态、已处理请求数等,当 master 进程要杀掉一个 worker 进程时则通过发送信号的方式通知 worker 进程。
55 0