简体   繁体   English

如何使用彗星(在nginx中禁用输出缓冲,它在apache中正常工作,但在nginx中不起作用)?

[英]How to use comet (disable output buffering in nginx, it works in apache normally but not nginx)?

I have a comet-driven chat script in my site 我的网站上有一个彗星驱动的聊天脚本

My Servers configuration is NGINX with PHP-FPM , I also have apache installed on different port. 我的服务器配置是带有PHP-FPM的NGINX,我还在不同的端口上安装了apache。

When I try to run the chat script on Apache and I do flood the buffer ( my output buffering size is 1 KB) when I flood it with 1024 character, it flushes automatically That's in apache. 当我尝试在Apache上运行聊天脚本时,当我用1024个字符填充缓冲区时,我确实填充了缓冲区(我的输出缓冲大小为1 KB),它会自动刷新。

But in nginx it doesn't. 但是在nginx中却没有。

My code is very similar to this 我的代码与此非常相似

<?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

}


?>

in my nginx configuration i turned gzip off, proxy_buffering off, 在我的Nginx配置中,我关闭了gzip,proxy_buffering,

is there a way to avoid buffering in nginx, I searched a lot here in stackoverflow but I couldn't reach to a solution 有没有一种方法可以避免在nginx中进行缓冲,我在stackoverflow中进行了很多搜索,但无法找到解决方案

and please notice: I don't want to turn off buffering in all of my php configuration I just want this to happen in the chat script 并且请注意:我不想在所有的php配置中关闭缓冲,我只是希望在聊天脚本中发生这种情况

Upgrade your nginx server {} config: 升级您的nginx服务器{}配置:

fastcgi_keep_conn on; # < solution

proxy_buffering off;
gzip off;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM