我一个统计程序估计要跑1分多钟以上php
查看了一个php-fpm 配置文件nginx
[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
很明显了sql
部分PHP程序的执行时间超过了Nginx的等待时间,能够适当增长nginx.conf配置文件中FastCGI的timeout时间数据库
google了一较之后vim
http://rtcamp.com/wordpress-nginx/tutorials/php/increase-script-execution-time/ app
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.wordpress
vim /etc/php5/fpm/php.ini
Set…php-fpm
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.post
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
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; #... }
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。
2、增长缓冲区容量大小
将nginx的error log打开,发现“pstream sent too big header while reading response header from upstream”这样的错误提示。查阅了一下资料,大意是nginx缓冲区有一个bug形成的,咱们网站的页面消耗占用缓冲区可能过大。参考老外写的修 改办法增长了缓冲区容量大小设置,502问题完全解决。后来系统管理员又对参数作了调整只保留了2个设置参数:client head buffer,fastcgi buffer size。
3、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秒去了