Caching Multiple Domain with Varnish
File to edit : "/etc/varnish/default.vcl"
## 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]