1. obj.ttl not accessible in method vcl_fetch 3. 如何不编写程序,使用Ctrl + F5清除varnish缓存 这在我们的生产环境中,是相当有用的。编辑上传图片后,直接按Ctrl + F5强制刷新web页面,即可刷新web页面包括其它资源。
具体的原理是当用户使用ctrl + F5强制刷新浏览器时,浏览器会添加 Pragma no-cache 这两行header, 那么只要在varnish中捕获这些header, 不就可以ctrl + F5清除缓存了吗?配置非常简单 在default.vcl 的 sub vcl_hit事件中增加以下代码:
我的代码是: sub vcl_hit {
这段代码告诉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) {
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" ) {
借助VCL强制的控制结构,可以做任何逻辑验证。 |
varnish常见错误的解决方法
最新推荐文章于 2023-10-29 08:55:34 发布