阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Varnish4.0缓存代理配置

181次阅读
没有评论

共计 6676 个字符,预计需要花费 17 分钟才能阅读完成。

varnish 缓存是 web 应用加速器,同时也作为 http 反向缓存代理。可以安装 varnish 在任何 http 的前端,同时配置它缓存内容。与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点。

Varnish 与 Squid 的对比

相同点:同是反向代理服务器、开源

Varnish 的优势:

1、Varnish 的稳定性很高,Squid 服务器发生故障的几率要高于 Varnish,因为使用 Squid 要经常重启
2、Varnish 访问速度更快,缓存数据都直接从内存读取,而 Squid 是从硬盘读取,所以 Varnish 要快于 Squid
3、Varnish 可以支持更多的并发连接,因为 Varnish 的 TCP 连接释放要比 Squid 快,因而在高并发连接情况下可以支持更多 TCP 连接
4、Varnish 可以通过管理端口,使用正则表达式批量的清除部分缓存,而 Squid 是做不到的;Squid 属于是单进程使用单核 CPU,但 Varnish 是通过 fork 形式打开多进程来做处理,所以可以合理的使用所有核来处理相应的请求

Varnish 的缺点:

1、varnish 进程一旦 Crash 或者重启,所有缓存数据都会丢失,在高并发下,给后端服务器造成很大压力
2、在 varnish 使用中如果单个 url 的请求通过 HA/F5 等负载均衡,则每次请求落在不同的 varnish 服务器中,造成请求都会被穿透到后端;而且同样的请求在多台服务器上缓存,也会造成 varnish 的缓存的资源浪费,造成性能下降

Varnish 劣势的解决方案:

缺点 1:在访问量很大的情况下推荐使用 varnish 的内存缓存方式启动,而且后面需要 跟多台 squid/nginx 服务器。主要为了防止前面的 varnish 服 务、服务器被重启的情况下,大量请求穿透 varnish,这样 squid/nginx 可以就担当第二层 CACHE,而且也弥补了 varnish 缓存在内存中重启都会释放的问题
缺点 2:可以在负载均衡上做 url 哈希,让单个 url 请求固定请求到一台 varnish 服务器上

Varnish 中内置子程序

vcl_recv 子程序:
开始处理请求,通过 return(动作); 选择 varnish 处理模式,默认进入 hash 缓存模式(即 return(hash);),缓存时间为配置项 default_ttl(默认为 120 秒)过期保持时间 default_grace(默认为 10 秒)。该子程序一般用于模式选择,请求对象缓存及信息修改,后端节点修改,终止请求等操作。

vcl_pipe 子程序:
pipe 模式处理,该模式主要用于直接取后端响应内容返回客户端,可定义响应内容返回客户端。该子程序一般用于需要及时且不作处理的后端信息,取出后端响应内容后直接交付到客户端不进入 vcl_deliver 子程序处理。

vcl_pass 子程序:
pass 模式处理,该模式类似 hash 缓存模式,仅不做缓存处理。

vcl_hit 子程序:
hash 缓存模式时,存在 hash 缓存时调用,用于缓存处理,可放弃或修改缓存。

vcl_miss 子程序:
hash 缓存模式时,不存在 hash 缓存时调用,用于判断性的选择进入后端取响应内容,可以修改为 pass 模式。

vcl_hash 子程序:
hash 缓存模式,生成 hash 值作为缓存查找键名提取缓存内容,主要用于缓存 hash 键值处理,可使用 hash_data(string) 指定键值组成结构,可在同一个页面通过 IP 或 cookie 生成不同的缓存键值。

vcl_purge 子程序:
清理模式,当查找到对应的缓存时清除并调用,用于请求方法清除缓存,并报告。

vcl_deliver 子程序:
客户端交付子程序,在 vcl_backend_response 子程序后调用(非 pipe 模式),或 vcl_hit 子程序后调用,可用于追加响应头信息、cookie 等内容。

vcl_backend_fetch 子程序:
发送后端请求之前调用,可用于改变请求地址或其它信息,或放弃请求。

vcl_backend_response 子程序:
后端响应后调用,可用于修改缓存时间及缓存相关信息。

vcl_backend_error 子程序:
后端处理失败调用,异常页面展示效果处理,可自定义错误响应内容,或修改 beresp.status 与 beresp.http.Location 重定向等。

vcl_synth 子程序:
自定义响应内容。可以通过 synthetic()和返回值 synth 调用,这里可以自定义异常显示内容,也可以修改 resp.status 与 resp.http.Location 重定向。

vcl_init 子程序:
加载 vcl 时最先调用,用于初始化 VMODs,该子程序不参与请求处理,仅在 vcl 加载时调用一次。

vcl_fini 子程序:
卸载当前 vcl 配置时调用,用于清理 VMODs,该子程序不参与请求处理,仅在 vcl 正常丢弃后调用。

附上一张原理图(摘自网络)

Varnish4.0 缓存代理配置

varnish 应用案例:

环境

web01(httpd):192.168.154.137
web02(httpd):192.168.154.138
varnish:192.168.154.139

步骤

web01&web02

yum -y install httpd
firewall-cmd –add-port=80/tcp –permanent
firewall-cmd –reload
setenforce 0

安装 varnish

下载地址:http://varnish-cache.org/releases/index.html
我使用的是 varnish-4.0.3.tar.gz 这个包

tar zxf varnish-4.0.3.tar.gz
cd varnish-4.0.3/
./configure && make && make install

/usr/local/var/varnish/default.vcl 文件的配置如下

# 加载后端负载均衡模块
import directors;
# 加载 std 模块
import std;
# 创建名为 backend_healthcheck 的健康检查策略
probe backend_healthcheck {
 .url=”/”;
 .interval = 5s;
 .timeout = 1s;
 .window = 5;
 .threshold = 3;
}
# 定义后端服务器
backend web_app_01 {
 .host = “192.168.154.137”;                    #这里改成你的 web 服务器地址
 .port = “80”;
 .first_byte_timeout = 9s;
 .connect_timeout = 3s;
 .between_bytes_timeout = 1s;
 .probe = backend_healthcheck;
}
backend web_app_02 {
 .host = “192.168.154.138”;                    #这里改成你的 web 服务器地址
 .port = “80”;
 .first_byte_timeout = 9s;
 .connect_timeout = 3s;
 .between_bytes_timeout = 1s;
 .probe = backend_healthcheck;
}
# 定义允许清理缓存的 IP
acl purgers {
 “127.0.0.1”;
 “localhost”;
 “192.168.154.0/24”;
}
#vcl_init 初始化子程序创建后端主机组
sub vcl_init {
 new web = directors.round_robin();
 web.add_backend(web_app_01);
 web.add_backend(web_app_02);
}
# 请求入口,用于接收和处理请求。这里一般用作路由处理,判断是否读取缓存和指定该请求使用哪个后端
sub vcl_recv {
 #将请求指定使用 web 后端集群 . 在集群名后加上 .backend()
 set req.backend_hint = web.backend();
 # 匹配清理缓存的请求
 if (req.method == “PURGE”) {
 if (!client.ip ~ purgers) {
 return (synth(405, “Not Allowed.”));
 }
 # 是的话就执行清理
 return (purge);
 }
 # 如果不是正常请求 就直接穿透没商量
 if (req.method != “GET” &&req.method != “HEAD” &&
 req.method != “PUT” &&
 req.method != “POST” &&
 req.method != “TRACE” &&
 req.method != “OPTIONS” &&
 req.method != “PATCH” &&
 req.method != “DELETE”) {
 return (pipe);
 }
 # 如果不是 GET 和 HEAD 就跳到 pass
 if (req.method != “GET” && req.method != “HEAD”) {
 return (pass);
 }
 #如果匹配动态内容访问请求就跳到 pass
 if (req.url ~ “\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)”) {
 return (pass);
 }
 #具有身份验证的请求跳到 pass
 if (req.http.Authorization) {
 return (pass);
 }
 if (req.http.Accept-Encoding) {
 if (req.url ~
 “\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$”) {
 unset req.http.Accept-Encoding;
 } elseif (req.http.Accept-Encoding ~ “gzip”) {
 set req.http.Accept-Encoding = “gzip”;
 } elseif (req.http.Accept-Encoding ~ “deflate”) {
 set req.http.Accept-Encoding = “deflate”;
 } else {
 unset req.http.Accept-Encoding;
 }
 }
 if (req.url ~
 “\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)”) {
 unset req.http.cookie;
 return (hash);
 }
 # 把真实客户端 IP 传递给后端服务器 后端服务器日志使用 X-Forwarded-For 来接收
 if (req.restarts == 0) {
 if (req.http.X-Forwarded-For) {set req.http.X-Forwarded-For = req.http.X-Forwarded-For + “, ” + client.ip;
 } else {
 set req.http.X-Forwarded-For = client.ip;
 }
 }
 return (hash);
}
# hash 事件(缓存事件)
sub vcl_hash {
 hash_data(req.url);
 if (req.http.host) {
 hash_data(req.http.host);
 } else {
 hash_data(server.ip);
 }
 return (lookup);
}
# 缓存命中事件
 sub vcl_hit {
 if (req.method == “PURGE”) {
 return (synth(200, “Purged.”));
 }
 return (deliver);
}
# 缓存不命中事件
 sub vcl_miss {
 if (req.method == “PURGE”) {
 return (synth(404, “Purged.”));
 }
 return (fetch);
}
# 返回给用户的前一个事件 通常用于添加或删除 header 头
sub vcl_deliver {
 if (obj.hits > 0) {
 set resp.http.X-Cache = “HIT”;
 set resp.http.X-Cache-Hits = obj.hits;
 } else {
 set resp.http.X-Cache = “MISS”;
}
# 取消显示 php 框架版本的 header 头
unset resp.http.X-Powered-By;
# 取消显示 web 软件版本、Via(来自 varnish) 等 header 头 为了安全
unset resp.http.Server;
unset resp.http.X-Drupal-Cache;unset resp.http.Via;
unset resp.http.Link;
unset resp.http.X-Varnish;
# 显示请求经历 restarts 事件的次数
set resp.http.xx_restarts_count = req.restarts;
# 显示该资源缓存的时间单位秒
set resp.http.xx_Age = resp.http.Age;
# 显示该资源命中的次数
set resp.http.hit_count = obj.hits;
# 取消显示 Age 为了不和 CDN 冲突
unset resp.http.Age;
# 返回给用户
return (deliver);
}
# pass 事件
sub vcl_pass {
return (fetch);
}
# 处理对后端返回结果的事件(设置缓存、移除 cookie 信息、设置 header 头等) 在 fetch 事件后自动调用
sub vcl_backend_response {
# 开启 grace 模式 表示当后端全挂掉后 即使缓存资源已过期(超过缓存时间) 也会把该资源返回给用户 资源最大有效时间为 5 分钟
set beresp.grace = 5m;
# 后端返回如下错误状态码 则不缓存
if (beresp.status == 499 || beresp.status == 404 || beresp.status == 502) {
set beresp.uncacheable = true;
}
# 如请求 php 或 jsp 则不缓存
if (bereq.url ~ “\.(php|jsp)(\?|$)”) {
set beresp.uncacheable = true;
} else {// 自定义缓存文件的缓存时长,即 TTL 值
if (bereq.url ~ “\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)”) {
set beresp.ttl = 15m;
unset beresp.http.Set-Cookie;
} elseif (bereq.url ~ “\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)”) {
set beresp.ttl = 30m;
unset beresp.http.Set-Cookie;
} else {
set beresp.ttl = 10m;
unset beresp.http.Set-Cookie;
}
}
# 返回给用户 return (deliver);
}
sub vcl_purge {
return (synth(200,”success”));
}
sub vcl_backend_error {
if (beresp.status == 500 ||
beresp.status == 501 ||
beresp.status == 502 ||
beresp.status == 503 ||
beresp.status == 504) {
return (retry);
}
}
sub vcl_fini {
return (ok);
}

启动 varnish

/usr/local/sbin/varnishd -f /usr/local/var/varnish/default.vcl -s malloc,100M -a 0.0.0.0:80
firewall-cmd –add-port=80/tcp –permanent
firewall-cmd –reload
setenforce 0

使用 /usr/local/sbin/varnishd - h 可以查看启动命令的帮助

然后通过 ss 或者 netstat 查看是否正常启动

##varnish 官网:http://varnish-cache.org/

##varnish 官方文档:http://varnish-cache.org/docs/index.html

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计6676字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中