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請求單獨設定一個後端