Nuster是一個基于HAProxy的高性能緩存服務器
https://github.com/jiangwenyuan/nuster
介紹
Nuster是一個基于HAProxy的高性能緩存服務器叁鉴。Nuster完全兼容HAProxy,并且利用
HAProxy的ACL功能來提供非常細致的緩存規(guī)則,比如
- 請求地址為某某時緩存
- 請求參數(shù)中的X為Y時緩存
- 響應頭中的X為Y時緩存
- 請求速率超過多少時緩存
- 等等
性能
非常快, 單進程模式下是nginx的3倍,多進程下nginx的2倍柳爽,varnish的3倍芦鳍。
安裝
make TARGET=linux2628
make install
具體參照HAProxy README
使用方法
在global中添加cache on
, 然后在backend或listen中添加cache filter和cache rule
指令
cache
syntax: cache on|off [data-size size] [dict-size size] [share on|off]
default: none
context: global
控制是否開啟緩存肩袍。
可以設置data-size來控制緩存數(shù)據(jù)的內存使用量焦辅〔┱龋可以使用m
, M
, g
和 G
.
默認是1MB,同時也是最小使用量筷登。只有http內容計算在內剃根,并不包括使用緩存帶來的內存開銷。
filter cache
syntax: filter cache [on|off]
default: on
context: backend, listen
定義一個cache filter, 另外cache-rule
也需要添加前方。
可以為多個代理添加狈醉,并單獨設置某個代理的緩存是否開啟。
如果定義了多個filter惠险,需要把cache filter放在最后苗傅。
cache-rule
syntax: cache-rule name [key KEY] [ttl TTL] [code CODE] [if|unless condition]
default: none
context: backend, listen
定義緩存規(guī)則≥航常可以同時定義多個金吗,但是需要注意順序,匹配則會停止測試趣竣。
acl pathA path /a.html
filter cache
cache-rule all ttl 3600
cache-rule path01 ttl 60 if pathA
path01
這條規(guī)則永遠不會執(zhí)行摇庙,因為all會匹配所有的規(guī)則。
name
定義一個名字遥缕。
key KEY
定義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
NAME
- cookie_NAME: the value of cookie
NAME
- param_NAME: the value of query
NAME
- body: the body of the request
默認key是method.scheme.host.path.query.body
Example
GET http://www.example.com/q?name=X&type=Y
http header:
GET /q?name=X&type=Y HTTP/1.1
Host: www.example.com
ASDF: Z
Cookie: logged_in=yes; user=nuster;
會得到:
- 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就會得到GEThttpwww.example.com/qname=X&type=Y
, 而
key method.scheme.host.path.header_ASDF.cookie_user.param_type
則會生成
GEThttpwww.example.com/qZnusterY
一個請求的key能在緩存中找到則返回緩存內容。
ttl TTL
定義key的失效時間单匣,可以使用 d
, h
, m
and s
夕凝。默認3600
秒.
如果不希望失效則設為0
code CODE1,CODE2...
默認只緩存200的響應,如果需要緩存其他的則可以添加户秤,all
會緩存任何狀態(tài)碼码秉。
cache-rule only200
cache-rule 200and404 code 200,404
cache-rule all code all
if|unless condition
定義ACL條件
詳見HAProxy configuration的7. Using ACLs and fetching samples
FAQ
如何調試?
在global
添加debug
, 或者帶-d
啟動haproxy
緩存相關的調試信息以[CACHE]
開頭
如何緩存POST請求?
添加option http-buffer-request
如果自定義了key的話需要使用body
關鍵字
請求body可能不完整鸡号,詳見HAProxy configuration 的
option http-buffer-request小節(jié)
另外可以為post請求單獨設置一個后端
Example
global
cache on data-size 100m
#daemon
## to debug cache
#debug
defaults
retries 3
option redispatch
timeout client 30s
timeout connect 30s
timeout server 30s
frontend web1
bind *:8080
mode http
acl pathPost path /search
use_backend app1a if pathPost
default_backend app1b
backend app1a
balance roundrobin
# mode must be http
mode http
# http-buffer-request must be enabled to cache post request
option http-buffer-request
acl pathPost path /search
# enable cache for this proxy
filter cache
# cache /search for 120 seconds. Only works when POST/PUT
cache-rule rpost ttl 120 if pathPost
server s1 10.0.0.10:8080
backend app1b
balance roundrobin
mode http
filter cache on
# cache /a.jpg, not expire
acl pathA path /a.jpg
cache-rule r1 ttl 0 if pathA
# cache /mypage, key contains cookie[userId], so it will be cached per user
acl pathB path /mypage
cache-rule r2 key method.scheme.host.path.query.cookie_userId ttl 60 if pathB
# cache /a.html if response's header[cache] is yes
http-request set-var(txn.pathC) path
acl pathC var(txn.pathC) -m str /a.html
acl resHdrCache1 res.hdr(cache) yes
cache-rule r3 if pathC resHdrCache1
# cache /heavy for 100 seconds if be_conn greater than 10
acl heavypage path /heavy
acl tooFast be_conn ge 100
cache-rule heavy ttl 100 if heavypage tooFast
# cache all if response's header[asdf] is fdsa
acl resHdrCache2 res.hdr(asdf) fdsa
cache-rule resCache ttl 0 if resHdrCache1
server s1 10.0.0.10:8080
frontend web2
bind *:8081
mode http
default_backend app2
backend app2
balance roundrobin
mode http
# disable cache on this proxy
filter cache off
cache-rule all
server s2 10.0.0.11:8080
listen web3
bind *:8082
mode http
filter cache
cache-rule everything
server s3 10.0.0.12:8080