从柳州访问OA系统下载附件速度非常慢,下载速度只有5KB/s左右,但从南宁的机房访问速度却很快,达到800KB/s,初步判断是柳州机房到桂林的网络问题。
无意中做了个测试,在nginx代理服务器上做个主机,指向同一主机的一个目录,目录内有个200M的iso文件,此时从柳州和南宁下载这个文件都有1Mb/s以上。断定是反向代理的原因。
再测试了反向代理到内网的一个hyper-v虚拟机和一个物理主机上的网站,在柳州下载都是只有几k,南宁下载有800kb/s左右。排除hyper-v主机机网络性能原因。
把nginx参数一个一个修改测试,最后在将 proxy_buffering 设置为 off 时,从柳州下载达到了1.5MB/s,从南宁机房下载有2.5MB/s。确定是代理缓存参数引起。
nginx官网对proxy_buffering参数是这么说的:
proxy_buffering
syntax: proxy_buffering on|off;
default: proxy_buffering on;
context: http, server, location
This directive activate response buffering of the proxied server.
If buffering is activated, then nginx reads the answer from the proxied server as fast as possible, saving it in the buffer as configured by directivesproxy_buffer_size and proxy_buffers. If the response does not fit into memory, then parts of it will be written to disk.
If buffering is switched off, then the response is synchronously transferred to client immediately as it is received. nginx does not attempt to read the entire answer from the proxied server, the maximum size of data which nginx can accept from the server is set by directive proxy_buffer_size.
Also note that caching upstream proxy responses won’t work if proxy_buffering is set to off.
For Comet applications based on long-polling it is important to set proxy_buffering to off, otherwise the asynchronous response is buffered and the Comet does not work.
Buffering can be set on a per-request basis by setting the X-Accel-Buffering header in the proxy response.
此参数默认值是on,意思是nginx从后端网站接收到数据后,先缓存起来,等buffer满了之后再发送给客户机,如果无法保存在缓冲区则会写入磁盘。将此参数关闭后,nginx从后端获取多少数据就发送多少到客户端。
问题虽然解决了,但还有困惑没解。
出处:blog.lizj.me
参考资料:
http://gcoder.blogbus.com/tag/proxy_buffering/
http://www.ithov.com/linux/109035.shtml