缓存对象不会在Varnish 4.1.1中过期

时间:2017-03-12 00:12:43

标签: caching varnish ttl

我遇到了Varnish 4.1.1的问题。我需要基于TTL的静态内容到期。 vcl_backend_response块具有以下设置:

opt[String]('r', "range").action { (x, c) =>
  val rx = "([0-9]+)\\-([0-9]+)".r
  val rx(from, to) = x
  c.copy(from = from.toInt, to = to.toInt)
}
// ...
println(s" Got range ${parsedArgs.from}..${parsedArgs.to}")

上面的代码工作正常,但对象不会在定义的TTL中过期。 Varnishstat代理中的存储参数不会更新该字段。不过,以下代码可以正常工作(清除所有缓存):

sub vcl_backend_response {
 if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
  unset beresp.http.Surrogate-Control;
  set beresp.do_esi = true;
}
if (beresp.status == 301 || beresp.status == 302) {
  set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", "");
}
if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
  return (abandon);
}
if (bereq.url ~ "^https?:\/\/(www\.)?sample\.com(\/.*)?$|^https?:\/\/((www\.)?(media|media1)\.)?sample\.com(\/.*)?$") { // This code filter my URL
if (bereq.url ~ "^[^?]*\.(css|js)(\?.*)?$") { // This code store css and js
  unset beresp.http.set-cookie;
  if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Surrogate-control ~ "no-store" || (!beresp.http.Surrogate-Control && beresp.http.Cache-Control ~ "no-cache|no-store|private") || beresp.http.Vary == "*") {
   set beresp.ttl = 1m; 
   set beresp.uncacheable = true;
   return (deliver);
  }
}
if (bereq.url ~ "^[^?]*\.(jpeg|jpg|gif|png)(\?.*)?$") {// This code store images
   unset beresp.http.set-cookie;
   if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Surrogate-control ~ "no-store" || (!beresp.http.Surrogate-Control && beresp.http.Cache-Control ~ "no-cache|no-store|private") || beresp.http.Vary == "*") {
    set beresp.ttl = 1m; 
    set beresp.uncacheable = true;
    return (deliver);
  }
}

我的问题是:清漆中的这种行为是否正常?

问候。

1 个答案:

答案 0 :(得分:1)

代码“上方”:

  • 仅适用于过去有过期的CSS和JS,具有Cookie或Cache-Control;或换句话说,告诉Varnish不要缓存它们
  • 将TTL覆盖为1分钟
  • 将其标记为“通过即可”并且不可缓存1分钟
  • 不必要的复杂

这将按您希望的方式工作:

sub vcl_backend_response {
    if (bereq.url ~ "^[^?]*\.(css|js)(\?.*)?$") { 
        unset beresp.http.set-cookie;
        set beresp.ttl = 1m; 
    }
    if (bereq.url ~ "^[^?]*\.(jpeg|jpg|gif|png)(\?.*)?$") {images
        unset beresp.http.set-cookie;
        set beresp.ttl = 1m; 
    }
}

基本建议 - 不要试图从互联网上复制粘贴内容而不试图了解其工作原理。你的VCL清楚地表明了大量的复制粘贴工作,而没有考虑到发生了什么。