Skip to content

Instantly share code, notes, and snippets.

@ibeeger
Last active February 7, 2019 11:31
Show Gist options
  • Save ibeeger/2080deb16284f11f057d1df95b72e4c9 to your computer and use it in GitHub Desktop.
Save ibeeger/2080deb16284f11f057d1df95b72e4c9 to your computer and use it in GitHub Desktop.
bigpipe 一些注意事项

最终结构 gzip on; //nginx 开启了 header('X-Accel-Buffering: no'); 头部禁用了 buffer

X-Accel-Buffering :暂时还没有彻底明白
https://crispgm.com/page/bigpipe-practice.html
http://www.cnblogs.com/cuchadanfan/p/6233601.html

大概场景:

 假如我们有个这样的服务器程序,每 100 毫秒会响应一个字符 "x",总共 32 个:
如果直接用浏览器访问这个 node 服务的端口 8000,会看到 "x" 是逐个出现的。一旦用 nginx 去 proxy_pass,并且没有设置 proxy_buffering,那么请求就会先陷入等待,然后所有的 "x" 再同时出现。只有将 proxy_buffering 设置为 off 之后才能得到和直接访问 node 服务端口一样的结果。
  并不是所有时候我们都改得了 nginx 配置的。假如 nginx 是别人家的,无法改配置怎么办?这也是有办法解决的!nginx 会识别一个叫 X-Accel-Buffering 的 HTTP 响应头,它的取值是 yes 和 no(注意不是 proxy_buffering 的 on 和 off)。所以我们在 node 代码中加入这个响应头就可以关闭掉 nginx 那一层 proxy_pass 的 proxy_buffering。而且 X-Accel-Buffering 的优先级比 nginx 配置中的 proxy_buffering 优先级更高,也就是说即使设置了 proxy_buffering 也会被 X-Accel-Buffering 给覆盖掉。
  中国有句老话叫道高一尺魔高一丈,nginx 这一层当然不能眼睁睁地把配置权限交给上游,它还可以通过 proxy_ignore_headers X-Accel-Buffering 的方式来忽略掉从上游服务器响应过来的特殊头对自己的影响。
  另外,对于 buffers 的具体配置,其实还有 proxy_buffers、proxy_buffer_size 之类的配置项可以影响。然而我并没有玩到这么细的程度,只是知道它们是什么而已,于是就不继续装逼了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment