最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php中file_get_contents()导致nginx出现504
时间:2022-06-24 19:55:31 编辑:袖梨 来源:一聚教程网
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'
要做到彻底解决,不妨重新封装一下file_get_contents函数:
代码如下 | 复制代码 |
function _file_get_content($str) { |
如此 用_file_get_content代替直接使用file_get_contents 问题解决。
相关文章
- 《交错战线》岗岩技能好用吗 06-16
- ChainCatcher 深度参与韩国加密盛会,助力 Web3 生态发展 06-16
- 安全可靠全球交易所加密货币APP排行-最安全现货交易所币安推荐 06-16
- 《斗罗大陆:史莱克学院》伙伴选择有哪些建议 06-16
- 《航海王壮志雄心》荣誉勋章获得方法有哪些 06-16
- 最新2025币圈十大虚拟币交易TOP10-最新比特币交易app币安推荐 06-16