How does Varnish cache handle different domains?

5,333

You can configure it to do so, sort-of. By default, it won't (and shouldn't).

From Varnish FAQ/HowDoI

I have a site with many hostnames, how do I keep them from multiplying the cache?

You can do this by normalizing the "Host" header for all your hostnames. Here's a VCL example:

if (req.http.host ~ "^(www.)?example.com") { set req.http.host = "example.com"; }

Share:
5,333

Related videos on Youtube

brianjob
Author by

brianjob

Updated on September 17, 2022

Comments

  • brianjob
    brianjob almost 2 years

    I just started reading about Varnish and am considering using it as a frontend to my webservers. I have multiple domains on my webserver that fetch the same content when the same query strings are used. So lets say one client visits:

    http://domain1.com/script.php?string1=abc&string2=123
    

    And later on, someone else visits:

    http://domain2.com/script.php?string1=abc&string2=123
    

    Would Varnish cache the results from the first visit and use that for the second client? Thanks!

    EDIT: After some more reading, it sounds like the following may work:

    sub vcl_hash {
        set req.hash += req.url;
        return (hash);
    }
    

    So instead of added the http.host variable to the hash, it ignores it.

  • Jeremy Stein
    Jeremy Stein almost 15 years
    That or it's probably possible to strip the hostname entirely and just use the remainder of the path. YMMV.
  • brianjob
    brianjob almost 15 years
    Hmm, I added some code above that may work. If anyone can confirm, that'd be great!
  • Lee B
    Lee B over 14 years
    I don't think (or don't hope) that Lin is asking about how to share the cache. The problem is that sharing the same data across urls on different sites could be a security/integrity issue. example.com/bobs_friends is NOT the same as example2.com/bobs_friends. Serving one when the other is asked for could be disastrous.