即使在Nginx中也可以使用PHP Flush [英] PHP Flush that works... even in Nginx
问题描述
是否可以在每次执行循环时回显?例如:
Is it possible to echo each time the loop is executed? For example:
foreach(range(1,9) as $n){
echo $n."\n";
sleep(1);
}
我不想看到循环结束时打印所有内容,而是希望每次打印每个结果.
Instead of printing everything when the loop is finished, I'd like to see it printing each result per time.
推荐答案
最终解决方案
这就是我所发现的:
Flush在Apache的mod_gzip或Nginx的gzip下不起作用,因为从逻辑上讲,它正在对内容进行gzip压缩,并且这样做必须缓冲内容才能对其进行gzip压缩.任何类型的Web服务器gzip压缩都会影响此效果.简而言之,在服务器端,我们需要禁用gzip并减小fastcgi缓冲区的大小.所以:
Flush would not work under Apache's mod_gzip or Nginx's gzip because, logically, it is gzipping the content, and to do that it must buffer content to gzip it. Any sort of web server gzipping would affect this. In short, at the server side, we need to disable gzip and decrease the fastcgi buffer size. So:
-
在php.ini中:
In php.ini:
. output_buffering =关闭
. output_buffering = Off
. zlib.output_compression =关闭
. zlib.output_compression = Off
在nginx.conf中:
In nginx.conf:
. gzip off;
. gzip off;
. proxy_buffering关闭;
. proxy_buffering off;
手头也有这句话,特别是如果您没有访问php.ini的话:
Also have this lines at hand, specially if you don't have acces to php.ini:
-
@ini_set('zlib.output_compression',0);
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ini_set('implicit_flush',1);
@ob_end_clean();
@ob_end_clean();
set_time_limit(0);
set_time_limit(0);
最后,如果有的话,请在下面写下代码:
Last, if you have it, coment the code bellow:
-
ob_start('ob_gzhandler');
ob_start('ob_gzhandler');
ob_flush();
ob_flush();
PHP测试代码:
ob_implicit_flush(1);
for($i=0; $i<10; $i++){
echo $i;
//this is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
sleep(1);
}
相关:
这篇关于即使在Nginx中也可以使用PHP Flush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!