◆案例1◆ 编译安装Varnish
1.安装依赖包
#yum install -y libtool ncurses-devel pcre-devel libxslt libedit python-imaging python-docutils yum install -y pcre-devel python-docutils libedit-dev*
2.编译安装Varnish
wget http://varnish-cache.org/_downloads/varnish-6.0.0.tgz tar -xzvf varnish-6.0.0.tgz cd varnish-6.0.0/ ./configure --prefix=/usr/local/varnish6 make && make install ln -s /usr/local/varnish6/sbin/* /usr/sbin/ ln -s /usr/local/varnish6/bin/* /usr/local/bin/ cp -a /usr/local/varnish6/share/doc/varnish/example.vcl /usr/local/varnish6/default.vcl
◆案例2◆ 使用Varnish实现负载均衡以及页面缓存
1.编辑Varnish主配置文件
编辑配置文件,在相应的区域追加写入以下标★语句
vim /usr/local/varnish/default.vcl 15 # Default backend definition. Set this to point to your content server. 16 backend default { 17 .host = "127.0.0.1"; 18 .port = "8080"; 19 } 20 ★ backend web1 { #均衡web主机1 ★ .host="192.168.1.13"; ★ .port="80"; #指定端口 ★ .probe = { #开启健康检查 ★ .url = "/"; #请求的URL路径 ★ .interval = 5s; #查询间隔时间 ★ .timeout = 1s; #超时时间 ★ .window = 5; #滑动窗 ★ .threshold = 3; #上次检查.window数量的多少 ★ } ★ } ★ backend web2 { ★ .host="192.168.1.14"; #均衡web主机2 ★ .port="80"; #指定端口 ★ .probe = { #开启健康检查 ★ .url = "/"; #请求的URL路径 ★ .interval = 5s; #查询间隔时间 ★ .timeout = 1s; #超时时间 ★ .window = 5; #滑动窗 ★ .threshold = 3; #上次检查.window数量的多少, ★ } ★ } ★ ★ import directors; #加载directors模块 ★ ★ sub vcl_init { #缓存及加速-03单-高性能缓存服务器 ★ ★ new bar = directors.round_robin(); ★ bar.add_backend(web1); ★ bar.add_backend(web2); ★ ★ } ★ ★ sub vcl_recv { ★ ★ set req.backend_hint = bar.backend(); #指定backend ★ ★ }
2.检查配置文件,并启动Varnish
varnishd -C -f /usr/local/varnish6/default.vcl #检查语法 varnishd -f /usr/local/varnish6/default.vcl #启动 pkill varnishd #关闭Varnish varnishlog #查看Varnish日志 netstat -anpt | grep varnishd #检查是否启动
3.验证环节
客户端访问http://127.0.0.1/ #varnish服务器会根据算法分配流量
◆案例3◆ Varnish实现动静分离实战
=====================================================================================
实验环境
[主机] [IP] [服务功能]
Varnish 10.1.10.65/16 Varnish Server
web1 10.1.10.66/16 httpd server
web2 10.1.10.67/16 httpd server
注意:在实现两台后端主机负载均衡时需将此路径设置为不缓存直接从后端主机中取得数据
=====================================================================================
配置动静分离配置如下
# This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and http://varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; import directors; probe check { #定义健康状态检测 .url = "/"; #检测的路径URL .window = 5; #检测次数 .threshold = 4; #检测次数中成功多少次才算健康 .interval = 2s; #两次健康状态检测之间的时间间隔 .timeout = 1s; #检测超时时长 } backend websrv1 { #添加后端主机websrv1 .host = "10.1.10.66"; #后端主机IP地址 .port = "80"; #后端主机监听的端口 .probe = check; #调用健康状态机制 } backend websrv2 { .host = "10.1.10.67"; .port = "80"; .probe = check; } sub vcl_init { #创建后端主机组 new websrv = directors.round_robin(); #设置主机组的调度算法,有两种,另一种为random websrv.add_backend(websrv1); #将后端主机加入到组中 websrv.add_backend(websrv2); #将后端主机加入到组中 } 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.url ~ "(?i)\.php$") { #将.php结尾的文件发往websrv1 set req.backend_hint = websrv1; } else { set req.backend_hint = websrv2; #将其他结尾的文件发往websrv1 } if (req.url ~"(?i)^/login") { #设置/login目录不检测缓存直接送达后端主机 return(pass); } if ( req.method == "PURGE") { #自定义purge方法 return(purge); } } sub vcl_purge { #调用purge方法,返回你想返回的状态码及其信息 return(synth(200,"Pugred.")); } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. if (beresp.http.cache.control !~ "s-maxage") { if (bereq.url ~ "(?i)\.jpg$") {? set beresp.ttl = 3600s; #设置.jpg结尾的图片TTL时长,加长其缓存时长 unset beresp.http.Set-Cookie; #取消追踪图片的cookie信息 } } if (beresp.http.cache.control !~ "s-maxage") { if (bereq.url ~ "(?i)\.css$") { set beresp.ttl = 3600s; unset beresp.http.Set-Cookie; } } } 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 = "Hit via "+ server.ip; }else { set resp.http.X-Cache = "Miss via "+ server.ip; } }