登录  /  注册
首页 > php教程 > php手册 > 正文

php中file_get_contents()导致nginx出现504

php中文网
发布: 2016-05-24 13:00:45
原创
2063人浏览过

nginx+php-cgi(php-fpm) 的web环境,突然发现系统负载上升,top 查看后发现很多 php-cgi 进程 cpu 使用率接近100%.

找其中一个 CPU 100% 的 php-cgi 进程的 PID,用strace -p 10747跟踪,结果发现以下结果:

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

select(7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0}) 

poll([{fd=6, events=POLLIN}], 1, 0) = 0 (Timeout) 

几乎可以肯定是file_get_contents()导致的问题.

原因是:file_get_contents的目标网站如果反应过慢,file_get_contents就会一直卡在那里不会超时,我们知道php.ini 里面max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效,真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的以下参数:

The timeout (in seconds) for serving a single request after which the worker process will be terminated   

Should be used when 'max_execution_time' ini option does not stop script execution for some reason   

'0s' means 'off'   

0s 

默认值为 0 秒,也就是说,PHP 脚本会一直执行下去,这样,当所有的 php-cgi 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,Nginx 将给用户返回"502 Bad Gateway",修改该参数,设置一个 PHP 脚本最大执行时间是必要的,但是,治标不治本,例如改成 30s,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-cgi 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免"502 Bad Gateway".

要做到彻底解决,不妨重新封装一下file_get_contents函数,代码如下:

function _file_get_content($str) {  
$ctx = stream_context_create(array(    
   'http' => array(    
   'timeout' => 10 //设置一个超时时间,单位为秒    
   )   //开源代码phprm.com 
   )    
);    
return file_get_contents($str, 0, $ctx);    
}
登录后复制

如此用_file_get_content代替直接使用file_get_contents 问题解决.


教程地址:

欢迎转载!但请带上文章地址^^

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号