Caching Multiple Domain with Varnish

24 / Mar / 2014 by abhishek.tomar 0 comments
Varnish is an in-memory web accelerator server which helps  Apache/Nginx server to run 10-300 times faster without spending anything on procuring hardware. Besides this It can also be used as a reverse proxy server.
In our projects we are using Varnish to cache the static content and in some cases we are also using it for caching the  full pages in the memory.  Few days back we got a requirement to cache multiple domain.
Mentioned below are the steps that we followed to configure the same.
File to edit : "/etc/varnish/default.vcl"
[shell]
## This allows to Setting up health check
probe varnish_probe {
.url = "/ping";
.timeout = 2s;
.interval = 5m;
.window = 3;
.threshold = 1;
}
[/shell]
[shell]
backend web1 {
.host = "site.abc.com";
.probe = varnish_probe;
}

backend web2 {
.host = "back.abc.com";
.probe = varnish_probe;
}

backend web3 {
.host = "site.abc.in";
.port = "8080";
}
[/shell]

For one of the website we are also using “Directors” to group multiple backends, which gives us incresed performance and resilience.

[shell]
## Following is a round robin director, which will be distributing the incoming requests on a round-robin basis. As we are also using the Health checks, so it will not send the incoming requests to unhealthy backend.
director prodweb round_robin {
{ .backend = web1; }
{ .backend = web2; }
}
[/shell]

Now we will edit the “vcl_recv” subroutine to hit the multiple backends as per our need.

[shell]
sub vcl_rcv {
if ( req.http.host ~ "^site.abc.com$" || req.http.host ~ "^back.abc.com$") {
set req.backend = prodweb; // we are using the director prodweb
}
if (req.http.host ~ "^site.abc.in$") {
set req.bakend = web3;
}
}
[/shell]

Now you can restart the varnish and test the same.
FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *