Search code examples
regexvarnishvarnish-vcl

Alter Varnish TTL based on Request URL


I have multiple subdomains pointing to one varnish instance. I read in the documentation that PCRE regex should be used. I believe the regex I have below should return true when the request url is “http://internal.my.com/any/thing” and the 15s ttl should be set. I’ve tried just (req.url ~ “internal.my.com”) as well because I read that it should match if any part of the request url contains that string. The below vcl_fetch subroutine is always resulting in 300s cache despite making a request to internal.my.com.

# Cache for a longer time if the internal.my.com URL isn't used
sub vcl_fetch {
  if (req.url ~ "^[(http:\/\/)|(https:\/\/)]*internal\.my\.com.*"){
    set beresp.ttl = 15 s;
  } else {
    set beresp.ttl = 300 s;
  }
}

Solution

  • Whoops... I should've used req.http.host instead of req.url. A simple misunderstanding when once corrected resulted in the intended behavior.