Search code examples
cachingvarnishvarnish-vcl

How do I make varnish cache subdomains as the same?


I want to serve/store the same cache files, regardless of subdomain, with the one exception of the api subdomain which has it's own cache.

So for example:

www.example.com us-1.example.com md-3.example.com

all have the same cache files served/stored, but api.example.com is separate.

The only thing I can think of is to overwrite the host header on the way back, but this is a problem because I need the backend to be able to determine and differentiate the subdomain that gets sent to it.

I'm new to varnish so I'm not quite sure how to do this. Thanks!


Solution

  • Just override vcl_hash to normalize the hostname:

    sub vcl_hash {
      hash_data(req.url);
      if (req.http.host == "api.example.com") {
        hash_data(req.http.host);
      }
      return (hash);
    }