支持http缓存 不支持HTTPS 需要配合nginx
Varnish Cache Varnish Cache 是一个为性能和灵活性而生的 web 加速器。它新颖的架构设计能带来显著的性能提升。根据你的架构,通常情况下它能加速响应速度300-1000倍。Varnish 将页面存储到内存,这样 web 服务器就无需重复地创建相同的页面,只需要在页面发生变化后重新生成。页面内容直接从内存中访问,当然比其他方式更快。 此外 Varnish 能大大提升响应 web 页面的速度,用在任何应用服务器上都能使网站访问速度大幅度地提升。 按经验,Varnish Cache 比较经济的配置是1-16GB内存+ SSD 固态硬盘。 其特性包括: 新颖的设计 VCL - 非常灵活的配置语言。VCL 配置会转换成 C,然后编译、加载、运行,灵活且高效 能使用 round-robin 轮询和随机分发两种方式来负载均衡,两种方式下后端服务器都可以设置权重 基于 DNS、随机、散列和客户端 IP 的分发器Director 多台后端主机间的负载均衡 支持 Edge Side Includes,包括拼装压缩后的 ESI 片段 重度多线程并发 URL 重写 单 Varnish 能够缓存多个虚拟主机 日志数据存储在共享内存中 基本的后端服务器健康检查 优雅地处理后端服务器“挂掉” 命令行界面的管理控制台 使用内联 C 语言来扩展 Varnish 可以与 Apache 用在相同的系统上 单个系统可运行多个 Varnish 支持 HAProxy 代理协议。该协议在每个收到的 TCP 请求——例如 SSL 终止过程中——附加一小段 http 头信息,以记录客户端的真实地址 冷热 VCL 状态 可以用名为 VMOD 的 Varnish 模块来提供插件扩展 通过 VMOD 定义后端主机 Gzip 压缩及解压 HTTP 流的通过和获取 神圣模式和优雅模式。用 Varnish 作为负载均衡器,神圣模式下可以将不稳定的后端服务器在一段时间内打入黑名单,阻止它们继续提供流量服务。优雅模式允许 Varnish 在获取不到后端服务器状态良好的响应时,提供已过期版本的页面或其它内容。 实验性支持持久化存储,无需 LRU 缓存淘汰 网站: www.varnish-cache.org
缓存类型:代理式缓存(递归方式) 旁挂式缓存(迭代) 缓存机制:过期机制 (Expires) 条件式缓存(通过最近文件修改时间戳或Etag的扩展标签来辨别)
过期时间:Expires HTTP/1.0 Expires:过期 HTTP/1.1 Cache-Control: maxage= (私有缓存,单位秒) Cache-Control: s-maxage= (公有缓存)
缓存层级: 私有缓存:用户代理附带的本地缓存机制 公共缓存:反向代理服务器的缓存功能
条件式请求: Last-Modified/If-Modified-Since:基于文件的修改时间戳来 判别:Etag/If-None-Match:基于文件的校验码来判别; User-Agent <--> private cache <--> public cache <--> public cache 2 <--> Original Server
Web Page Cache:squid --> varnish
应用运维 发布 -------------变更 --------故障处理
服务 /usr/lib/systemd/system/varnish.service /usr/lib/systemd/system/varnishlog.service /usr/lib/systemd/system/varnishncsa.service
配置接口 VCL VCL vcl complier --> c complier --> shared object
varnish的程序环境: /etc/varnish/varnish.params: 配置varnish服务进程的工作特性,例如监听的地址和端口,缓存机制; /etc/varnish/default.vcl:配置各Child/Cache线程的缓存策略;
主程序:/usr/sbin/varnishd
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
(默认6082为varnish管理端口)
Shared Memory Log交互工具: /usr/bin/varnishhist /usr/bin/varnishlog /usr/bin/varnishncsa /usr/bin/varnishstat /usr/bin/varnishtop
测试工具程序: /usr/bin/varnishtest VCL配置文件重载程序:/usr/sbin/varnish_reload_vcl
varnish服务 /usr/lib/systemd/system/varnishlog.service /usr/lib/systemd/system/varnishncsa.service 日志持久的服务;
两类配置 配置varnishd守护进程 配置缓存系统 : vcl
state engine vcl_recv vcl_pipe vcl_hash vcl_hit vcl_miss vcl_pass vcl_purge vcl_synth vcl_deliver
key , value
key -> keys_zone neicun
value -> disk cipan
root@centos74 ~# vim /etc/varnish/varnish.params
前面为面向客户端 后面为定义服务端
修改完成后 加载服务 不要重启
保存退出后 进入varnish varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 管理端口
公共缓存 各个客户端都能连接缓存
#必须以vcl git 开头
vcl 4.0;
#导入directors模块实现负载均衡;并定义acl,来控制purger(更新缓存)的使用
import directors;
acl purgers {
"127.0.0.0"/8;
}
#健康状态检查定义项
probe healthche { .url="/index.html"; .timeout = 2s; .window = 6 ; .threshold = 5; } #backend组即为后台web端 backend server1 { .host = "172.18.64.7"; .port = "80"; .probe = healthche ; } backend server2 { .host="172.18.64.106"; .port="80"; .probe = healthche ; }
}
#以轮询方式调度
sub vcl_init {
new srvs =directors.round_robin();
srvs.add_backend(server1);
srvs.add_backend(server2);
}
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.
set req.backend_hint=srvs.backend();
#正则匹配 login或admin隐私信息不允许服务端缓存
if (req.url ~ "(?i)^/(login|admin)") {
return(pass);
}
#url重写,告诉后端服务器真实的请求者,安全避免重复添加,还可定义在记录日志中
if (req.restarts == 0) {
if (req.http.X-Fowarded-For) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
#purge的使用:更新一个缓存,而更新一组缓存用ban
if (req.method == "PURGE"){
if (!client.ip ~ purgers) {
return(synth(405,"Purging not allowed for "+client.ip));
}
return(purge);
}
}
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.
#客户端的图片类信息可以除去cookies标志让服务器能够缓存,并定义缓存有效期为2H
if (bereq.url ~ "(?i).(jpg|jpeg|png|gif)$") {
unset beresp.http.Set-cookies;
set beresp.ttl =7200s;
}
}
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 from " + server.ip;
}
}
1
2 图片类 取消私有标识 并强行设定其可以由varnish缓存时长 定义在vcl_backend_respons中
3 传递真正访问的ip地址
sub vcl_recv { 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; } } if (req.url ~ "(?i)^/(login|admin)") { return(pass); } }
缓存修剪 purge一次一个 ban停止
调用组
并在76 和 77 httpd服务上相同的目录下建立相同的文件 且内容不同
随机算法 权重为一个数组 调度 只要样本够大 就能看到是3:1
Windows 基于最近的多少次检查来判断其健康状态 threshold 最近.windows 中定义的这么多次检查中至少有 .threshold定义的次数是成功的 timeout 超时时长 .expected_response 期望的响应码 默认为200