强化 PHP 脚本失败,带有“指定的超时已过期”错误/ap_content_length_filter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intensive PHP script failing w/ "The timeout specified has expired" error / ap_content_length_filter
提问by sbuck
Running a MySQL intensive PHP script that is failing. Apache log reports this:
运行失败的 MySQL 密集型 PHP 脚本。Apache 日志报告如下:
[Wed Jan 13 00:20:10 2010] [error] [client xxx.xx.xxx.xxxx] (70007)
The timeout specified has expired:
ap_content_length_filter: apr_bucket_read() failed,
referer: http://domain.com/script.php
Tried putting set_time_limit(0)at the top.
尝试放在set_time_limit(0)顶部。
Also tried set_time_limit(0)
也试过 set_time_limit(0)
Neither fixed the timeout.
两者都没有修复超时。
Is there some specific timeout limit I can up in http.conf(or elsewhere) to prevent this?
我可以在http.conf(或其他地方)设置一些特定的超时限制来防止这种情况吗?
回答by misterich
I hit a very similar wall as well with Apache 2.4.6and PHP 5.4.23 FPM/FastCGI.
我在Apache 2.4.6和PHP 5.4.23 FPM/FastCGI 上也遇到了非常相似的问题。
Symptom:
症状:
No matter what I set in PHP or Apache, my script would timeout in 30 seconds and I would see the following in my Apache Error log:
无论我在 PHP 还是 Apache 中设置什么,我的脚本都会在 30 秒内超时,并且我会在我的 Apache 错误日志中看到以下内容:
[timestamp] [proxy_fcgi:error] [pid...] (70007)The timeout specified has expired: [client ...] AH01075: Error dispatching request to :
[timestamp] [proxy_fcgi:error] [pid...] (70007)The timeout specified has expired: [client ...] AH01075: Error dispatching request to :
My VirtualHost:
我的虚拟主机:
TimeOut 300
KeepAliveTimeout 300
<IfModule reqtimeout_module>
RequestReadTimeout header=120-240,minrate=500
RequestReadTimeout body=120,minrate=500
</IfModule>
<IfModule mod_proxy.c>
ProxyTimeout 300
</IfModule>
<IfModule mod_fcgid.c>
FcgidConnectTimeout 300
</IfModule>
The pesky php script:
讨厌的php脚本:
ini_set( 'max_execution_time', '120' );
...
ini_restore( 'max_execution_time' );
The Fix: it's a hard coded value in Apache mod_proxy_fcgi
修复:它是 Apache 中的硬编码值mod_proxy_fcgi
- A patchis available (link above)
- The fix doesn't appear to be slated for general release yet (Mar 2014)
- 有补丁可用(上面的链接)
- 该修复程序似乎尚未正式发布(2014 年 3 月)
回答by nemesisfixx
First, my solution is only applicable to the Apache Web Server.
首先,我的解决方案仅适用于 Apache Web Server。
I am working on a script meant to act as a csv download script for a report against a very very large db, and I encountered this problem too. Am NOT using php, but instead my script is written in some obscure language called heitml ;-)
我正在编写一个脚本,用作针对非常大的数据库的报告的 csv 下载脚本,我也遇到了这个问题。我没有使用 php,而是我的脚本是用一种叫做 heitml 的晦涩语言编写的;-)
The request timeout proble does occur in my scenario like this:
请求超时问题确实发生在我的场景中,如下所示:
[Wed Sep 19 20:29:01 2012] [warn] [client ::1] Timeout waiting for output from CGI script /var/www/cgi-bin/heitml
[Wed Sep 19 20:29:01 2012] [error] [client ::1] (70007)The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed
And the only serious solution I can currently adapt to is using this official timeout config extension here : mod_reqtimeout. It allows adjustment of timeout params like for example:
我目前可以适应的唯一严肃的解决方案是在此处使用此官方超时配置扩展:mod_reqtimeout。它允许调整超时参数,例如:
Allow 10 seconds to receive the request including the headers and 30 seconds for receiving the request body:
允许 10 秒接收包含标头的请求和 30 秒接收请求正文:
RequestReadTimeout header=10 body=30
Allow at least 10 seconds to receive the request body. If the client sends data, increase the timeout by 1 second for every 1000 bytes received, with no upper limit for the timeout (exept for the limit given indirectly by LimitRequestBody):
等待至少 10 秒来接收请求正文。如果客户端发送数据,则每接收 1000 个字节将超时增加 1 秒,超时没有上限(除了 LimitRequestBody 间接给出的限制):
RequestReadTimeout body=10,MinRate=1000
Allow at least 10 seconds to receive the request including the headers. If the client sends data, increase the timeout by 1 second for every 500 bytes received. But do not allow more than 30 seconds for the request including the headers:
允许至少 10 秒来接收包含标头的请求。如果客户端发送数据,则每接收 500 个字节将超时增加 1 秒。但不允许请求包括标头超过 30 秒:
RequestReadTimeout header=10-30,MinRate=500
Usually, a server should have both header and body timeouts configured. If a common configuration is used for http and https virtual hosts, the timeouts should not be set too low:
通常,服务器应该同时配置标头和正文超时。如果 http 和 https 虚拟主机使用通用配置,则超时不应设置得太低:
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
Am yet to find out whether there's a better solution offered by Apache that doesn't require me to use an this module (assuming it's not installed by default -- though it's included in all versions 2.2.15 and later).
我还没有发现 Apache 是否提供了不需要我使用这个模块的更好的解决方案(假设默认情况下没有安装它——尽管它包含在所有版本 2.2.15 及更高版本中)。
回答by Tim Smith
I suspect I'm getting the same error, updated for a later version of Apache (2.4.16):
我怀疑我遇到了同样的错误,已针对更高版本的 Apache (2.4.16) 进行了更新:
[Tue Aug 02 11:49:41.930884 2016] [core:error] [pid 28640] (70007)The timeout specified has expired: [client xxx.xxx.xxx.xxx:xxxxx] AH00574: ap_content_length_filter: apr_bucket_read() failed, referer: https://domain.com/script.php
I was wondering why increasing max_execution_time in php.ini wasn't working.
我想知道为什么在 php.ini 中增加 max_execution_time 不起作用。
For me, the fix was simply increasing the Timeout directive in httpd.conf https://httpd.apache.org/docs/2.4/mod/core.html#timeout
对我来说,修复只是增加 httpd.conf https://httpd.apache.org/docs/2.4/mod/core.html#timeout 中的超时指令
Timeout 900
(Or in WHM-> Apache Configuration-> Global Configuration, as the case may be)
(或在WHM-> Apache Configuration-> 中Global Configuration,视情况而定)
This timeout applies to time between IO events. So even though the script was outputting data almost immediately, a long delay in the middle of the script's execution was causing the timeout.
此超时适用于 IO 事件之间的时间。因此,即使脚本几乎立即输出数据,脚本执行过程中的长时间延迟也会导致超时。
回答by leepowers
There's also the php max_execution_timedirective. Note that the web server's timeout settings may also be limiting your script:
还有 php max_execution_time指令。请注意,Web 服务器的超时设置也可能会限制您的脚本:
Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.
您的 Web 服务器可能有其他超时配置,这些配置也可能会中断 PHP 执行。Apache 有一个 Timeout 指令,IIS 有一个 CGI 超时功能。两者都默认为 300 秒。有关特定详细信息,请参阅您的 Web 服务器文档。
Actually, this looks like an Apache error, it also effects Python scripts. Have you tried googling it yet?
实际上,这看起来像是 Apache 错误,它也会影响 Python 脚本。你试过谷歌搜索吗?
回答by dmc
There is another timeout value placed not in php itself but in apache server. It will brake script when nothing is on output for specified time so when doing harder work in PHP you can reach this limit. Just echo anything back to browser (not buffers!) or increase apache timeout value to safe value, as far as I remember it's KeepAliveTimeOut apache property. Good luck :)
还有另一个超时值不是放在 php 本身而是放在 apache 服务器中。当在指定时间内没有任何输出时,它会停止脚本,因此当在 PHP 中做更难的工作时,你可以达到这个限制。只需将任何内容回显到浏览器(不是缓冲区!)或将 apache 超时值增加到安全值,据我所知它是 KeepAliveTimeOut apache 属性。祝你好运 :)
回答by Chris
Note:This answer is duplicated verbatim from a similar question.
注意:这个答案是从一个类似的问题逐字复制的。
I have Apache 2.4.6, but the patchto fix it is provided in Apache >= 2.4.8. The key here is to startyour output immediately so that Apache (mod_proxy_fcgi) thinks the connection is active.
我有 Apache 2.4.6,但在 Apache >= 2.4.8 中提供了修复它的补丁。这里的关键是立即开始输出,以便 Apache (mod_proxy_fcgi) 认为连接处于活动状态。
For example, I am using PHP and the DB query for my AJAX call takes > 30 seconds. Because I know that the overall response will be "Content-Type: application/json", I send that header immediately.
例如,我使用 PHP 并且我的 AJAX 调用的数据库查询需要 > 30 秒。因为我知道总体响应将是“Content-Type: application/json”,所以我立即发送该标头。
#1: Start output immediately
#Note: Sending the header is innocuous
# it can be changed later using the $replace parameter
# (see #3)
header( 'Content-Type: application/json' );
#2: Run slow query
mysql_query( "SELECT * FROM giant_table" );
#3: Change header as needed
header( 'Content-Type: application/csv', true );
#output content
回答by sbuck
I played around with these resource limits in php.ini to correct the problem.
我在 php.ini 中使用了这些资源限制来解决这个问题。
max_execution_time = 300
max_input_time = 300
memory_limit = -1
回答by Jeff Beck
There is a timeout in the php.inias well.
也有超时php.ini。
回答by Floaz
I tried all suggestions, but my problem was in the php-fpm configuration. I set the following line in my php-fpm configuration file:
我尝试了所有建议,但我的问题出在php-fpm 配置中。我在 php-fpm 配置文件中设置了以下行:
request_terminate_timeout = 300s

