1. obj.ttl not accessible in method vcl_fetch
这是因为从2.0.6以后,obj.ttl 已经变更为beresp.ttl
2. beresp.cacheable的含义是什么?
官方的解释:beresp.cacheable
True if the request resulted in a cacheable response. A response is considered cacheable if HTTP status code is 200, 203, 300, 301, 302, 404 or 410 and pass wasn’t called in vcl_recv. If however, both the TTL and the grace time for the response are 0 beresp.cacheable will be 0.
beresp.cacheable is writable.
如果后端服务器返回的状态码是200,203,300,302,404, 410并且没有在vcl_recv事件中没有返回pass,则beresp.cacheable为true
当然,如果后端机器返回内容的ttl 和grace time 如果都是0, 则beresp.cacheable也就为0
并且beresp.cacheable在vcl配置中是可改写的。
3. 如何不编写程序,使用Ctrl + F5清除varnish缓存
这在我们的生产环境中,是相当有用的。编辑上传图片后,直接按Ctrl + F5强制刷新web页面,即可刷新web页面包括其它资源。
具体的原理是当用户使用ctrl + F5强制刷新浏览器时,浏览器会添加
Pragma no-cache
Cache-Control no-cache
这两行header, 那么只要在varnish中捕获这些header, 不就可以ctrl + F5清除缓存了吗?配置非常简单
在default.vcl 的 sub vcl_hit事件中增加以下代码:
if ( req.http.Pragma ~ "no-cache" ) {
set obj.ttl = 0s ;
return (pass);
}
我的代码是:
sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
if ( req.http.Pragma ~ "no-cache" ) {
set obj.ttl = 0s ;
return (pass);
}
return (deliver);
}
这段代码告诉varnish, 当请求header Pragma中包含no-cache时,清理缓存(obj.ttl=0s),并直接从后端服务器取得数据( 即return (pass) );
但是这样一来,任何人使用Ctrl + F5即可清除缓存,存在一定问题,可考虑使用IP来源限制,如以下代码:
acl local {
"192.168.0.25";
"1.2.3.4";
}
sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
if (client.ip ~ local && req.http.Pragma ~ "no-cache") {
set obj.ttl = 0s;
return (pass);
}
return (deliver);
}
也可以使用Cookie传递密码方式验证,只有Cookie中传递的密码正确,才清除缓存。
准备工作:给firefox安装 Modify Header插件,设置Cookie, 比如设置Cookie : pwd=123456, 则varnish的配置:
if ( req.http.Cookie ~ "pwd=123456" && req.http.Pragma ~ "no-cache" ) {
set obj.ttl = 0s ;
return (pass);
}
借助VCL强制的控制结构,可以做任何逻辑验证。
分享到:
相关推荐
varnish 安装包 varnish 安装包 varnish 安装包 varnish 安装包
linux-varnish配置
varnish+lighttpd配置
本资源包含Varnish的安装文件varnish-3.0.5-1.el6.x86_64.rpm 和varnish-libs-3.0.5-1.el6.x86_64.rpm,是搭建cdn的高性能HTTP加速器Varnish服务器必需的安装包,下载后解压即可,具体安装配置过程清移步我的博客
varnish 官方白皮书文档,包含配置信息,说明信息,方便查阅使用。
varnish测试报告
laravel-varnish, 使 varnish 和 Laravel 在一起很好 使 varnish 和 Laravel 在一起很好 这个包提供了一个简单的方法,可以在 Laravel 中使用 varnish 4 ( 或者 5 ) 。 它提供了路由中间件,在应用到路由时,将确保 ...
Varnish权威指南(中文)
Varnish 2.1.2 缓存负载,做反向代理缓存优于squid
varnish-magento
Getting Started with Varnish Cache: Accelerate Your Web Applications by Thijs Feryn English | 13 Mar. 2017 | ASIN: B06XP3D6Z2 | 253 Pages | AZW3 | 720.91 KB How long does it take for your website to ...
教程名称: Varnish配置教程和学习资料合集【】HTTP加速器varnish安装部署【】varnish cache 配置使用ChinaUnix【】varnish 原理【】Varnish-vcl的配置【】varnish配置实例 资源太大,传百度网盘了,链接在附件中,...
squid、varnish、ngx_cache的性能测试对比报告
Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。 本文档详细讲解了varnish的安装配置过程以及配置文件详解
nginx和php和varnish配置
在反向代理,web加速方面,varnish已经有足够能力代替squid。挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。 2.作者:Poul-Henning Kamp是FreeBSD的内核开发者之一。
varnish的VCL的详细解析,网上整理
varnish4,是目前varnish最新版本。
最近,朋友托我帮他找些关于Varnish的资料。整理了下,都在这里了。... |- 使用Varnish代替Squid做网站缓存加速器的详细解决方案 |- Varnish_cache.pdf(中文版) |- 在Varnish架构上编程的注意点(中文)-Varnish开发者