当前位置: 首页 > news >正文

基于HAProxy的高性能缓存服务器nuster

Nuster是一个基于HAProxy的高性能缓存服务器

https://github.com/jiangwenyu...

介绍

Nuster是一个基于HAProxy的高性能缓存服务器。Nuster完全兼容HAProxy,并且利用
HAProxy的ACL功能来提供非常细致的缓存规则,比如

  • 请求地址为某某时缓存
  • 请求参数中的X为Y时缓存
  • 响应头中的X为Y时缓存
  • 请求速率超过多少时缓存
  • 等等

性能

非常快, 单进程模式下是nginx的3倍,多进程下nginx的2倍,varnish的3倍。

详见benchmark

安装

make TARGET=linux2628
make install

具体参照HAProxy README

使用方法

global中添加cache on, 然后在backendlisten中添加cache filter和cache rule

指令

cache

syntax: cache on|off [data-size size]

default: none

context: global

控制是否开启缓存。
可以设置data-size来控制缓存数据的内存使用量。可以使用m, M, gG.
默认是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

定义缓存规则。可以同时定义多个,但是需要注意顺序,匹配则会停止测试。

acl pathA path /a.html
filter cache
cache-rule all ttl 3600
cache-rule path01 ttl 60 if pathA

path01这条规则永远不会执行,因为all会匹配所有的规则。

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会缓存任何状态码。

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小节

另外可以为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

相关文章:

  • [20171106]配置客户端连接注意.txt
  • 分享Silverlight/WPF/Windows Phone一周学习导读(07月25日-07月31日)
  • Python类的一般形式和继承
  • ArcGIS 10.5 新功能
  • 局域网内手机播放视频
  • Ubuntu Server 10.10 操作手记
  • JavaScript 基本功--面试宝典
  • Oracle Number用法
  • Linux下随机10字符病毒的清除
  • wbadmin执行备份命令
  • Oracle Study之案例--通过IPCS查看共享内存之“怪现象”
  • DVDROM驱动不能加载的问题
  • 【驱动】linux设备驱动·扫盲
  • 对Context的重新思考
  • 在RHEL5下构建LAMP网站服务平台之架设Discuz!论坛
  • CentOS7 安装JDK
  • css选择器
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • express + mock 让前后台并行开发
  • iOS | NSProxy
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • Vim Clutch | 面向脚踏板编程……
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 聚类分析——Kmeans
  • 利用jquery编写加法运算验证码
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 容器服务kubernetes弹性伸缩高级用法
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 使用agvtool更改app version/build
  • 写代码的正确姿势
  • 因为阿里,他们成了“杭漂”
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • AI算硅基生命吗,为什么?
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​​​​​​​ubuntu16.04 fastreid训练过程
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • !!Dom4j 学习笔记
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • $.ajax()参数及用法
  • (16)Reactor的测试——响应式Spring的道法术器
  • (39)STM32——FLASH闪存
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (八)Spring源码解析:Spring MVC
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (译)2019年前端性能优化清单 — 下篇
  • (转)mysql使用Navicat 导出和导入数据库
  • (转)可以带来幸福的一本书
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • .net core Swagger 过滤部分Api
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .net 生成二级域名