关于Nginx均衡磁盘IO的问题解决
数据在服务器之间使用 lsyncd进行实时同步(效果相当不错)
前端使用nginx proxy来做load balance 配置相当简单, 过程具体就不说了
部署完毕后,功能上一切正常,唯独proxy服务器的load average巨高,查看发现Proxy服务器的Write I/O 占总消耗的90%以上。
这里比较纳闷, 因为业务本身很少有写操作, iotop查看消耗全在nginx上,但怎么也想不通什么东西如此消耗I/O,于是打开nginx error log并设置等级为debug。
观察了一段时间,发现每次request都要先在proxy本地找一下缓存,没有的时候才去请求真实服务器,其实配置中我并没有设置proxy_stroe之类的参数,因为本身就是要负载I/O ,如果都Cache到proxy上 那就达不到真正的目的了,更何况Cache不太适合大量的不算小的文件。
看了一下 nginx wiki proxy章发现以下选项:
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 assumes the answer of the proxied server as fast as possible, preserving it in the buffer, assigned by directive proxy_buffer_size
and proxy_buffers
.
If the response can not all be placed in 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 do not attempt to count entire answer of the proxied server, maximum size of data, which nginx can accept from the server it is assigned by directive proxy_buffer_size
.
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.
总算发现问题所在了,设置off重启, 一切正常。
这么说来如果是单纯拿来做负载均衡而不做代理,此项应该是必关的,否则只有无谓的消耗。
但我在google上搜索 "proxy_buffering", 结果只有3000+条, 除了nginx中文wiki之外,其他再无中文介绍, 搜索 "nginx load balance" 确有3W+ , 大多数都讲proxy如何配置,千篇一律 。对这样的参数只字未提, 很是费解.....