-->

如何禁用为nginx的PHP应用程序输出缓冲(How to disable output buffe

2019-06-27 11:51发布

我们有类似下面的代码:

<?php
    ob_implicit_flush(true);
    ob_end_flush();

    foreach ($arrayOfStrings as $string) {
        echo time_expensive_function($string);
    }
?>

在Apache中,这将互送回显到浏览器,因为它是输出。 在nginx的/的FastCGI然而,这并不因为他TOT方式nginx的作品(默认)工作。

是否有可能作出的nginx / FastCGI的这项工作,如果是这样,怎么样?

Answer 1:

上述解决方案都没有为我工作。

首先PHP已经正确地刷新的一切:

@ob_end_flush();
@flush();

然后,我发现了两个工作方案:

1)通过Nginx的配置:

fastcgi_buffering off;

2)通过在php代码HTTP报头

header('X-Accel-Buffering: no');


Answer 2:

简单的解决方案:

fastcgi_keep_conn on; # < solution

proxy_buffering off;
gzip off;


Answer 3:

我不希望有关闭的gzip整个服务器或整个目录,只是几个剧本,在少数特定情况下。

所有你需要的是这样的东西是echo'ed之前:

header('Content-Encoding: none;');

然后做冲水正常:

ob_end_flush();
flush();

Nginx的似乎挑上已经关闭了编码,也不gzip压缩。



Answer 4:

添加的flush()函数在循环:

foreach ($arrayOfStrings as $string) {
  echo time_expensive_function($string);
  flush();
}

也许工作,但不一定在每次迭代(有一些神奇的参与!)



Answer 5:

在-flush加入FastCGI的配置,请参考手册:

  • http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiServer

  • http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiConfig

  • http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer

从http://mailman.fastcgi.com/pipermail/fastcgi-developers/2009-July/000286.html



文章来源: How to disable output buffering in nginx for PHP application