如何使用comet(在nginx中禁用输出缓冲,它通常在apache中工作,但不能在nginx中工作)?

She*_*bic 1 php comet nginx output-buffering

我的网站上有一个以彗星为主导的聊天脚本

我的服务器配置是带有PHP-FPM的NGINX,我也在不同的端口上安装了apache.

当我尝试在Apache上运行聊天脚本时,我将缓冲区(我的输出缓冲区大小为1 KB)当我用1024个字符填充它时,它会自动刷新它在apache中.

但在nginx中却没有.

我的代码与此非常相似

<?php

// this is to fill the buffer and start output; and it works on apache normally
echo str_repeat(" ",1024); 


while($condition){

  // Some code here...
  $messages = getMessagesFromDatabase();

 if($messages){
  echo "output";   // output works on apache but not nginx
  flush();
  ob_flush();
 }

 usleep(500000); // 0.5 Second

}


?>
Run Code Online (Sandbox Code Playgroud)

在我的nginx配置中,我关闭了gzip,关闭了proxy_buffering,

有没有办法避免在nginx中缓冲,我在stackoverflow中搜索了很多,但我无法达成解决方案

请注意:我不想在我的所有php配置中关闭缓冲我只想在聊天脚本中发生这种情况

Ond*_*zka 6

升级你的nginx服务器{}配置:

fastcgi_keep_conn on; # < solution

proxy_buffering off;
gzip off;
Run Code Online (Sandbox Code Playgroud)