varnish-学习总结(反向代理缓存、负载均衡)

本文详细介绍了Varnish作为反向代理和Web加速器的应用,包括其基本概念、配置方法及如何实现单一服务器缓存和多服务器负载均衡。通过实际案例,展示了如何在不同场景下配置Varnish,以提高网站响应速度和处理能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念

  1. varnish:【Varnish HTTP Cache】,Varnish是反向HTTP代理,有时也称为HTTP加速器或Web加速器。 反向代理是一种代理服务器,在客户端看来是普通服务器。 Varnish在内存中存储(缓存)文件或文件片段,以减少响应时间和对未来等效请求的网络带宽消耗。 根据现代硬件,现代操作系统和现代工作负载而设计。
  2. varnish官网:http://varnish-cache.org/
  3. 后端服务器:【backend】也叫原点服务器,后端服务器将提供 varnish 加速的内容,在配置文件/etc/varnish/default.vcl中;Varnish 可以定义多个后端服务器,并且可以通过定义多个后端服务器达到负载均衡的目的。
  4. varnish默认使用端口:6081
  5. VCL:【varnish configuration language】,varnish区域配置语言,目的在于规定请求的处理和内容的缓存策略。在执行vcl时,varnish 就把 VCL 转换成二进制代码

反向代理缓存

单一服务器

  1. 试验内容:node1反向代理缓存node2主机
  2. 试验环境:2台机器
主机名IP地址服务作用
node1192.168.27.11varnish反向代理缓存
node2192.168.27.12httpdhttp服务
  1. 具体步骤:
    1. node1中安装varnish,本次安装varnish-6.3.1-1.el7.x86_64.rpm;依赖性软件jemalloc-3.6.0-1.el7.x86_64.rpm
    2. 修改配置文件/etc/varnish/default.vcl,添加代理后端服务器
    # Default backend definition. Set this to point to your content server.
    backend default {
    	   .host = "192.168.27.12";  	#将node2地址设置为varnish后端服务
    	   .port = "80";							#访问端口80
    }
    
    1. 更改端口6081为80;(因为使用的是rhel7.5系统,服务是由有systemd管理,此处修改varnish服务启动后的访问端口方式为;修改/usr/lib/systemd/system/varnish.service服务文件)
    ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6081 -f /etc/varnish/default.vcl -s malloc,256m
    #execstart 真实启动方式,调用启动脚本varnishd,execstart=后面的可以直接在命令行中运行,开启服务
    #-a varnish监听所有发给80端口的http请求;也可以写作-a 0.0.0.0:80
    #-T 转发本地监听端口6081
    #-f 指定配置文件为/etc/varnish/default.vcl
    #-s 指定选项用来确定 varnish 使用的存储类型和存储容量,使用的是 malloc 类型( malloc 是一个 C 函数,用于分配内存空间), 1G 定义多少内存被 malloced
    
    1. node2安装httpd,设置访问页面index.html内容为node2
  2. 测试内容
    1. 确认反向代理可否进行访问
    # node1中:
    curl 192.168.27.11
    node2
    
    1. 确认是本地缓存,还是转发地址(此处需要添加vcl_deliver;vcl_deliver当一个没有被 cached 内容交付给客户端的时候被调用
      node1中修改配置文件/etc/varnish/default.vcl,重启服务
    #是否存在访问记录
    sub vcl_deliver {
        # Happens when we have all the pieces we need, and are about to send the
        # response to the client.
        #
        # You can do accounting or modifying the final object here.
    if (obj.hits > 0){								#检测访问次数
            set resp.http.X-Cache = "HTML cache";	#已有访问记录反馈信息
    }
    else {
            set resp.http.X-Cache = "MISS cache";	#未访问过记录反馈信息
    }
    return (deliver);
    }
    
    测试结果:
    #为保证测试内容准备,清除所有缓存记录,保证测试环境纯净
    varnishadm ban req.url "~" /
    #第一次测试没有缓存内容
    [root@node1 ~]# curl -I 192.168.27.11
    HTTP/1.1 200 OK
    Date: Thu, 30 Jan 2020 18:59:37 GMT
    Server: Apache/2.4.6 (Red Hat Enterprise Linux)
    Last-Modified: Thu, 30 Jan 2020 17:40:24 GMT
    ETag: "6-59d5ef3a97cab"
    Content-Length: 6
    Content-Type: text/html; charset=UTF-8
    X-Varnish: 2
    Age: 0
    Via: 1.1 varnish (Varnish/6.3)
    X-Cache: MISS cache								#显示MISS cache 证明没有缓存
    Accept-Ranges: bytes
    Connection: keep-alive
    #第二次测试已使用缓存内容
    [root@node1 ~]# curl -I 192.168.27.11
    HTTP/1.1 200 OK
    Date: Thu, 30 Jan 2020 18:59:37 GMT
    Server: Apache/2.4.6 (Red Hat Enterprise Linux)
    Last-Modified: Thu, 30 Jan 2020 17:40:24 GMT
    ETag: "6-59d5ef3a97cab"
    Content-Length: 6
    Content-Type: text/html; charset=UTF-8
    X-Varnish: 5 3
    Age: 3
    Via: 1.1 varnish (Varnish/6.3)
    X-Cache: HTML cache
    Accept-Ranges: bytes							#显示HTML cache 证明使用缓存内容
    Connection: keep-alive
    

多台服务器

  1. 试验内容:node2和node3分别为后端http服务,node1反向代理node2与node3,并通过vcl_recv进行转发判断(vcl_recv在请求开始的时候被调用,当一个完整的请求被接受到,并被解析,它的作用就是决定是否给这个请求提供服务,怎么服务,如果服务,哪个后端会被选取
  2. 试验环境:3台机器
主机名IP地址服务作用
node1192.168.27.11varnish负载均衡
node2192.168.27.12httpdhttp服务
node3192.168.27.13httpdhttp服务
  1. 具体步骤:
    1. node1中安装varnish,本次安装varnish-6.3.1-1.el7.x86_64.rpm;依赖性软件jemalloc-3.6.0-1.el7.x86_64.rpm
    2. 修改配置文件/etc/varnish/default.vcl,添加多台后端服务器
    # Default backend definition. Set this to point to your content server.
    backend node2 {		#配置node2转发服务
        .host = "192.168.27.12";
        .port = "80";
    }
    backend node3 {		#配置node3转发服务
        .host = "192.168.27.13";
        .port = "80";
    }
    
    1. 添加vcl_recv判断规则
    sub vcl_recv {
        # Happens before we check if we have this in cache already.
        #
        # Typically you clean up the request here, removing cookies you don't need,
        # rewriting the request, etc.
        if(req.http.host ~ "^(www.)?test.org"){		#配置检测访问为test.org或着为www.test.org时
    	set req.http.host ="www.test.org";		#查询主机内容均为www.test.org
    	set req.backend_hint= node2;				#将node2转发给访问客户端
    	}
        elsif(req.http.host ~ "bbs.test.org"){	#检测访问bbs.test.org时
    	set req.backend_hint=node3;					#将node3转发给访问客户端
        }	
        else{
    	return (synth(405));						#其他访问返回405
        }
    }
    
    1. 更改端口6081为80;(因为使用的是rhel7.5系统,服务是由有systemd管理,此处修改varnish服务启动后的访问端口方式为;修改/usr/lib/systemd/system/varnish.service服务文件)
    ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6081 -f /etc/varnish/default.vcl -s malloc,256m
    #execstart 真实启动方式,调用启动脚本varnishd,execstart=后面的可以直接在命令行中运行,开启服务
    #-a varnish监听所有发给80端口的http请求;也可以写作-a 0.0.0.0:80
    #-T 转发本地监听端口6081
    #-f 指定配置文件为/etc/varnish/default.vcl
    #-s 指定选项用来确定 varnish 使用的存储类型和存储容量,使用的是 malloc 类型( malloc 是一个 C 函数,用于分配内存空间), 1G 定义多少内存被 malloced
    
    1. node2安装httpd,设置访问页面index.html内容为node2;node3安装httpd,设置访问页面index.html内容为node3
  2. 测试内容
    [root@node1 ~]# curl www.test.org
    node2												#访问www.test.org 转发至node2
    [root@node1 ~]# curl bbs.test.org
    node3												#访问bbs.test.org 转发至node3
    [root@node1 ~]# curl node1							
    <!DOCTYPE html>
    <html>
      <head>
        <title>405 Method Not Allowed</title>
      </head>
      <body>
        <h1>Error 405 Method Not Allowed</h1>			#访问其他 返回405
        <p>Method Not Allowed</p>
        <h3>Guru Meditation:</h3>
        <p>XID: 5</p>
        <hr>
        <p>Varnish cache server</p>
      </body>
    </html>
    

负载均衡

  1. 试验内容:node2和node3分别为后端http服务,node1使用varnish负载均衡,通过添加调度模块实现轮询
  2. 试验环境:3台机器
主机名IP地址服务作用
node1192.168.27.11varnish负载均衡
node2192.168.27.12httpdhttp服务
node3192.168.27.13httpdhttp服务
  1. 具体步骤:
    1. node1中安装varnish,本次安装varnish-6.3.1-1.el7.x86_64.rpm;依赖性软件jemalloc-3.6.0-1.el7.x86_64.rpm
    2. 修改配置文件/etc/varnish/default.vcl,添加多台后端服务器
    import directors from "/usr/lib64/varnish/vmods/libvmod_directors.so";			#添加调度模块
    
    # Default backend definition. Set this to point to your content server.
    backend node2 {																	#配置node2转发服务
        .host = "192.168.27.12";
        .port = "80";
    }
    backend node3 {																	#配置node3转发服务
        .host = "192.168.27.13";
        .port = "80";
    }
    
    sub vcl_init {																	#加载vcl时最先调用,用于初始化VMODs,该子程序不参与请求处理,仅在vcl加载时调用一次。
    	new lb = directors.round_robin();											#轮询方式round_robin,一人一次
    	lb.add_backend(node2);
    	lb.add_backend(node3);
    }
    
    
    1. 添加vcl_recv判断规则
    sub vcl_recv {
        # Happens before we check if we have this in cache already.
        #
        # Typically you clean up the request here, removing cookies you don't need,
        # rewriting the request, etc.
        if(req.http.host ~ "^(www.)?test.org"){		#配置检测访问为test.org或着为www.test.org时
    	set req.http.host ="www.test.org";			#查询主机内容均为www.test.org
    	set req.backend_hint= lb.backend();			#将以轮询方式将node2和node3转发给访问客户端
    	return(pass);								#注意此处为重点,不要漏写,防止varnish缓存内容,若缓存内容不能体现轮询效果
    	}
        else{
    	return (synth(405));						#其他访问返回405
        }
    }
    
    1. 更改端口6081为80;(因为使用的是rhel7.5系统,服务是由有systemd管理,此处修改varnish服务启动后的访问端口方式为;修改/usr/lib/systemd/system/varnish.service服务文件)
    ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6081 -f /etc/varnish/default.vcl -s malloc,256m
    #execstart 真实启动方式,调用启动脚本varnishd,execstart=后面的可以直接在命令行中运行,开启服务
    #-a varnish监听所有发给80端口的http请求;也可以写作-a 0.0.0.0:80
    #-T 转发本地监听端口6081
    #-f 指定配置文件为/etc/varnish/default.vcl
    #-s 指定选项用来确定 varnish 使用的存储类型和存储容量,使用的是 malloc 类型( malloc 是一个 C 函数,用于分配内存空间), 1G 定义多少内存被 malloced
    
    1. node2安装httpd,设置访问页面index.html内容为node2;node3安装httpd,设置访问页面index.html内容为node3
  2. 测试内容
    [root@node1 ~]# curl www.test.org
    node2
    [root@node1 ~]# curl www.test.org
    node3										#负载均衡,轮询效果
    [root@node1 ~]# curl www.test.org
    node2
    [root@node1 ~]# curl www.test.org
    node3
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值