varnish常见错误的解决方法
来源:互联网 发布:thinkpad 散热知乎 编辑:程序博客网 时间:2024/05/19 20:40
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缓存503错误,400错误的原因和解决方法
- varnish缓存503错误,400错误的原因和解决方法
- varnish 400 503错误的原因及解决方法
- hibernate的常见错误解决方法
- Android - 常见错误的解决方法
- iPhone - 常见错误的解决方法
- Android - 常见错误的解决方法
- Myeclipse常见的错误解决方法
- Android - 常见错误的解决方法
- iPhone - 常见错误的解决方法
- 别人的JSP常见错误的解决方法
- 常见的80004005错误及其解决方法
- 硬盘的常见错误提示及解决方法
- 回滚段的常见错误及解决方法
- 操作CoreData 常见的错误及解决方法
- DB2常见的错误以及解决方法
- IAR常见的错误及解决方法
- MFC文件操作大全(一)
- 论调用约定__stdcall,__cdecl,__fastcall,thiscall,naked call
- 分析BootstrapClassLoader/ExtClassLoader/AppClassLoader的加载路径 及"父委托机制"
- text to ps for printing
- 我对抽象类和接口的理解
- varnish常见错误的解决方法
- oracle HWM原因和解决方案(待完善)
- flex 移动开发
- linux文件系统权限
- Tomcat 系统架构与设计模式
- 利用jpeglib把bmp图片转为jpg
- IntrospectorCleanupListener类
- HTML5 新特性学习
- 走出程序员的悲哀+自己的一点感受