Nginx环境下flush失效问题

最近在用comethttp long poll请求,发现使用nginx反向代理时response.flushBuffer()失效了,请求一直返回504,而直接连接tomcat不会出现问题。

高开nginx日志,出现error:

   upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /app/conn?cmd=revival&cid=96c6fc98-8101-4aa7-8482-f319615abc63&ram=0.9041435304097831 HTTP/1.1", upstream: "http://127.0.0.1:8280/app/conn?cmd=revival&cid=96c6fc98-8101-4aa7-8482-f319615abc63&ram=0.9041435304097831", host: "t.ganlanren.com", referrer: "http://***/app/index.html"

 

从错误找不到任何线索,返回去调试代码,发现nginx代理下response.flushBuffer()数据无法返回到客户端,通过抓包发现数据已经返回到nginx,而nginx未返回到客户端,再去看Nginx,有如下的设置:

  fastcgi_buffer_size 128k;
  fastcgi_buffers 8 128k;

问题基本找到了,Nginx会缓冲http输出的信息,当达到128k时才会将缓冲区的数据发送给客户端,那么我们首先需要将这个缓冲区调小,比如:

  fastcgi_buffer_size 4k;
  fastcgi_buffers 8 4k;

并且,必须禁用gzip

  gzip off;

调整nginx配置后测试,一切ok

你可能感兴趣的:(nginx)