How to use comet (disable output buffering in ngin

2019-08-04 13:11发布

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.

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.

But in nginx it doesn't.

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,

is there a way to avoid buffering in nginx, I searched a lot here in stackoverflow but I couldn't reach to a solution

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

1条回答
别忘想泡老子
2楼-- · 2019-08-04 13:47

Upgrade your nginx server {} config:

fastcgi_keep_conn on; # < solution

proxy_buffering off;
gzip off;
查看更多
登录 后发表回答