0

I have an issue with Varnish 4.1.1. I need expire static content based in TTL. The vcl_backend_response block have the following settings:

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);
  }
}

The above code works fine but the objects don´t expire based in the TTL defined. The storage parameter in Varnishstat Agent doesn´t update the field. Nevertheless, the following code does works correctly (clear all cache):

sub vcl_backend_response {
   set beresp.ttl = 1m;
   return(deliver);
}

My question is: Is it normal this behavior in varnish?

Regards.

1 Answer 1

1

The code "above":

  • works only for CSS and JS which have Expires in the past, have Cookie or Cache-Control; or in other words, which tell Varnish not to cache them
  • overrides TTL to 1 min
  • marks them as hit-for-pass and not cacheable for 1 minute
  • unnecessarily complex

This will work as you want it to work:

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; 
    }
}

Basic advice - don't try to copy paste things from Internet without trying to understand how things work. Your VCL clearly indicates heavy copy paste work without thinking little on what's going on.

1
  • Your last comment clearly indicates heavy patronizing tone without thinking little about the OP Commented Mar 23, 2018 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.