Apache 超时指令正确但请求永不超时(不起作用)

Apache 超时指令正确但请求永不超时(不起作用)

我无法让 Timeout 指令正常工作,我已在以下说明和官方 Docker 容器中重现了该问题。服务器使用具有默认配置的标准 MPM prefork 模型。我的理解是,超时后 Timeout 应该返回 500 错误或某种 4xx 或 5xx 响应代码,但事实并非如此。我的理解是否不正确?

设置:

mkdir /tmp/test; cd /tmp/test
echo "<?php sleep(10);" > index.php
docker run -d -p 80:80 --name apache-php -v "/tmp/test":/var/www/html php:5.6-apache
time curl http://localhost/ # should take 10 seconds

现在,将超时时间降低到 1 秒:

docker cp apache-php:/etc/apache2/apache2.conf . # Copy to host, since container doesn't have editor.  
echo "Timeout 1" >> apache2.conf # Set Timeout to 1, default is 300.  
docker cp apache2.conf apache-php:/etc/apache2/apache2.conf # Copy conf file back to container  
docker exec -it apache-php /bin/bash
service apache2 reload
exit # exit docker container. 

测试错误:

time curl http://localhost/ # Here is where I expect a timeout error, but it works just the same as before and takes 10 seconds.

ps 我读到过,AcceptFilter 可以影响事物,直到请求通过它为止。我们也有以下配置:

AcceptFilter http none
AcceptFilter https none

答案1

Apache 超时是指 TCP 超时,而不是脚本执行时间。PHP 超时通过位于 php.ini 中的两个指令控制:

来自 php.net

max_input_time = integer

This sets the maximum time in seconds a script is allowed to parse input   
data, like POST and GET. Timing begins at the moment PHP is invoked at the
server and ends when execution begins. The default setting is -1, which means      
that max_execution_time is used instead. Set to 0 to allow unlimited time.

并且(这可能是你需要的)

max_execution_time = integer

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server.    
The default setting is 30. When running PHP from the command line the default setting is 0.   
The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.   
You can not change this setting with ini_set() when running in safe mode. The only workaround is to turn off safe mode or by changing the time limit in the php.ini.   
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.   

相关内容