All Linux HowTo's

Configure Varnish Cache for Multiple Domains – CentOS/Redhat 6

Share This:

This article explains how to configure the Varnish Cache to cache for multiple domains on the same backend server. I’ve used CentOS 6.2 for this example. In this example, Varnish is listening on TCP port 80. The DNS “www” “A” record for the domains “site1.com.au” and “site2.com.au” point to the Varnish server. So web browsers instructed to load the website “site1.com.au” will end up at the Varnish server. The Varnish server will then go collect the content from the real web server located at (in this example) IP address 1.2.3.4.

yum install varnish

Edit “/etc/sysconfig/varnish”:

VARNISH_LISTEN_PORT=80

Add the following to the “/etc/varnish/default.vcl” file, just below the “default” section. Replace the “.host” value in the “server1″ section with the IP address of the backend server hosting your websites. Also replace “site1.com.au” and “site2.com.au” with the websites that you want to cache:

# Server 1.2.3.4 is where the websites are hosted.
backend server1 {
  .host = "1.2.3.4";
  .port = "80";
}

sub vcl_recv {
if (req.http.host ~ "site1.com.au$") {
  set req.backend = server1;
 }
 elseif (req.http.host ~ "site2.com.au$") {
  set req.backend = server1;
 }
 else {
  set req.backend = default;
 }

 if (req.http.Host ~ "(?i)^(www.)?site1.com.au/^$") {
  return (lookup);
 }
 if (req.http.Host ~ "(?i)^(www.)?site2.com.au/^$") {
  return (lookup);
 }
}

Restart Varnish and test it:

service varnish restart

Remember to open the port that Varnish is listening on. If it’s an abnormal port, check SELinux too.

Similar Posts: