Nuster是一个基于HAProxy的高性能缓存服务器
https://github.com/jiangwenyuan/nuster
介绍
====
Nuster是一个基于HAProxy的高性能缓存服务器。Nuster完全兼容HAProxy,并且利用
HAProxy的ACL功能来提供非常细致的缓存规则,比如
请求地址为某某时缓存
请求参数中的X为Y时缓存
响应头中的X为Y时缓存
请求速率超过多少时缓存
等等
性能
非常快, 单进程模式下是nginx的3倍,多进程下nginx的2倍,varnish的3倍。
详见benchmark
安装
具体参照HAProxy README
使用方法
=======
在global中添加<code>cache on</code>, 然后在backend或listen中添加cache filter和cache rule
指令
cache
syntax: cache on|off data-size size share on|off
default: none
context: global
控制是否开启缓存。
可以设置data-size来控制缓存数据的内存使用量。可以使用<code>m</code>, <code>M</code>, <code>g</code> 和 <code>G</code>.
默认是1MB,同时也是最小使用量。只有http内容计算在内,并不包括使用缓存带来的内存开销。
filter cache
syntax: filter cache on|off
default: on
context: backend, listen
定义一个cache filter, 另外<code>cache-rule</code>也需要添加。
可以为多个代理添加,并单独设置某个代理的缓存是否开启。
如果定义了多个filter,需要把cache filter放在最后。
cache-rule
syntax: cache-rule name key KEY code CODE
定义缓存规则。可以同时定义多个,但是需要注意顺序,匹配则会停止测试。
<code>path01</code>这条规则永远不会执行,因为all会匹配所有的规则。
定义一个名字。
定义key,由以下关键字组成:
method: http method, GET/POST...
scheme: http or https
host: the host in the request
path: the URL path of the request
query: the whole query string of the request
header_NAME: the value of header <code>NAME</code>
cookie_NAME: the value of cookie <code>NAME</code>
param_NAME: the value of query <code>NAME</code>
body: the body of the request
默认key是<code>method.scheme.host.path.query.body</code>
Example
会得到:
method: GET
scheme: http
host: www.example.com
path: /q
query: name=X&type=Y
header_ASDF: Z
cookie_user: nuster
param_type: Y
body: (empty)
所以默认的key就会得到<code>GEThttpwww.example.com/qname=X&type=Y</code>, 而
<code>key method.scheme.host.path.header_ASDF.cookie_user.param_type</code>则会生成
<code>GEThttpwww.example.com/qZnusterY</code>
一个请求的key能在缓存中找到则返回缓存内容。
定义key的失效时间,可以使用 <code>d</code>, <code>h</code>, <code>m</code> and <code>s</code>。默认<code>3600</code>秒.
如果不希望失效则设为0
默认只缓存200的响应,如果需要缓存其他的则可以添加,<code>all</code>会缓存任何状态码。
定义ACL条件
详见HAProxy configuration的7. Using ACLs and fetching samples
FAQ
===
如何调试?
在<code>global</code>添加<code>debug</code>, 或者带<code>-d</code>启动<code>haproxy</code>
缓存相关的调试信息以<code>[CACHE]</code>开头
如何缓存POST请求?
添加<code>option http-buffer-request</code>
如果自定义了key的话需要使用<code>body</code>关键字
请求body可能不完整,详见HAProxy configuration 的
option http-buffer-request小节
另外可以为post请求单独设置一个后端