Nuster是一款基于HAProxy的高性能缓存服务器https://github.com/jiangwenyu...介绍Nuster是一款基于HAProxy的高性能缓存服务器。Nuster完全兼容HAProxy,利用HAProxy的ACL功能提供了非常详细的缓存规则,比如当请求地址一定时,缓存请求参数中的X为Y,缓存响应头中的X为Y,当缓存请求率超过多少缓存等性能非常快,单进程模式下nginx的3倍,多进程模式下nginx的2倍,varnish的3倍。详见benchmark安装makeTARGET=linux2628makeinstall参考HAProxyREADME使用方法将cacheon添加到global,然后添加cachefilter和cacherule命令cachesyntax:cacheon|off[data-sizesize]default:nonecontexttobackend或listen:全局控制是否启用缓存。可以设置data-size来控制缓存数据的内存使用。可以使用m、M、g、G,默认1MB,也是最小使用量。只统计http内容,不包括使用缓存带来的内存开销。filtercachesyntax:filtercache[on|off]default:oncontext:backend,listen定义了一个缓存过滤器,还需要加上cache-rule。您可以添加多个代理,并设置是否启用某个代理的缓存。如果定义了多个过滤器,缓存过滤器需要放在最后。cache-rulesyntax:cache-rulename[keyKEY][ttlTTL][codeCODE][if|unlesscondition]default:nonecontext:backend,listen定义缓存规则。可以同时定义多个,但是需要注意顺序,匹配到就会停止测试。aclpathApath/a.htmlfiltercachecache-ruleallttl3600cache-rulepath01ttl60ifpathApath01永远不会执行这条规则,因为all会匹配所有规则。名称定义名称。keyKEY定义key,由以下关键字组成:method:http方法,GET/POST...scheme:http或httpshost:requestpath中的host:requestquery的URL路径:requestheader_NAME的整个查询字符串:标头的值NAMEcookie_NAME:cookie的值NAMEparam_NAME:查询的值NAMEbody:请求的主体默认键是method.scheme.host.path.query.bodyExampleGEThttp://www.example.com/q?name=X&type=Yhttpheader:GET/q?name=X&type=YHTTP/1.1Host:www.example.comASDF:ZCookie:logged_in=yes;用户=用户名;会得到:method:GETscheme:httphost:www.example.compath:/qquery:name=X&type=Yheader_ASDF:Zcookie_user:nusterparam_type:Ybody:(empty)所以默认key会得到GEThttpwww.example.com/qname=X&type=Y,和keymethod.scheme.host.path.header_ASDF.cookie_user.param_type会生成GEThttpwww.example.com/qZnusterY请求的key可以在缓存中找到并返回缓存内容。ttlTTL定义密钥的过期时间,可以使用d、h、m和s。默认为3600秒。如果你不想让它失效,就把它设置为0codeCODE1,CODE2...默认情况下,只缓存200个响应。如果需要缓存其他响应,可以添加它们。All将缓存任何状态代码。cache-ruleonly200cache-rule200and404code200,404cache-ruleallcodeallif|unlesscondition定义ACL条件详见HAProxy配置7.使用ACL和抓样FAQ如何调试?在全局添加debug,或者用-d启动haproxy缓存调试信息以[CACHE]开头如何缓存POST请求?添加选项http-buffer-request如果自定义key,需要使用body关键字请求body可能不完整。有关详细信息,请参阅HAProxy配置的选项http-buffer-request部分。为post请求设置一个单独的后端Exampleglobalcacheondata-size100m#daemon##todebugcache#debugdefaultsretries3optionredispatchtimeoutclient30stimeoutconnect30stimeoutserver30sfrontendweb1bind*:8080modehttpaclpathPostpath/searchuse_backendapp1aifpathPostdefault_backendapp1bbackendapp1abalanceroundrobin#modemustbehttpmodehttp#http-buffer-requestmustbeenabledtocachepostrequestoptionhttp-buffer-requestaclpathPostpath/search#enablecacheforthisproxyfiltercache#cache/search持续120秒。仅当POST/PUT缓存规则rpostttl120ifpathPost服务器s110.0.0.10:8080backendapp1b平衡循环模式http过滤器缓存在#cache/a.jpg上时有效,不会过期aclpathA路径/a.jpg缓存规则r1ttl0ifpathA#cache/mypage,keycontainscookie[userId],所以会缓存每个用户aclpathBpath/mypagecache-ruler2keymethod.scheme.host.path.query.cookie_userIdttl60ifpathB#cache/a.htmlifresponse'sheader[cache]isyeshttp-requestset-var(txn.pathC)pathaclpathCvar(txn.pathC)-mstr/a.htmlaclresHdrCache1res.hdr(cache)yescache-ruler3ifpathCresHdrCache1#cache/heavy100secondsifbe_conngreaterthan10aclheavypagepath/heavyacltooFastbe_connge100cache-ruleheavyttl100ifheavypagetooFast#cacheallifresponse'sheader[asdf]isfdsaaclresHdrCache2res.hdr(asdf)fdsa缓存规则resCachettl0如果resHdrCache1servers110.0.0.10:8080frontendweb2bind*:8081modehttpdefault_backendapp2backendapp2balanceroundrobinmodehttp#disablecacheonthisproxyfiltercacheoffcache-ruleallservers210.0.0.11:8080listenweb3bind*:8082modehttp过滤器缓存缓存规则一切服务器s310.0.0.12:8080
