读书人

varnish常见异常的解决办法

发布时间: 2012-09-15 19:09:28 作者: rapoo

varnish常见错误的解决方法

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强制的控制结构,可以做任何逻辑验证。

?

读书人网 >互联网

热点推荐