How to make a virtual host?

Bjørn Ruberg bjorn at ruberg.no
Thu Aug 19 10:35:58 CEST 2010


On 08/19/2010 10:08 AM, H.Päiväniemi wrote:
> Hi,
>
> I'm a varnish-newbie.
>
> How on the earth can I make a virtual host in varnish?
>
> I would like to, for example:
>
> * make a default virtual host with a backend A
>
> * make a virtual host "host1.foo.bar" with a backend B
>
> * make a virtual host "host2.foo.bar" with a backend C
>
> ...and of course there would be different caching rules in all of those.
>
> But how?

Varnish doesn't need any particular definitions of virtual hosts, but 
will look for the "Host:" header in the HTTP requests and can make 
decisions based on that.

After you have predefined your backends A, B and C, you can do something 
like this:

sub vcl_recv {

     set req.backend = A; # This is the default backend unless
			 # another backend is set

     if (req.http.host == "host1.foo.bar") {
         set req.backend = B;
	# Add your caching rules for host1.foo.bar here

     } elsif (req.http.host )) "host2.foo.bar") {
	set req.backend = C;
	# Add your caching rules for host2.foo.bar here

     }
}

You may need to specify different caching rules in vcl_fetch as well, YMMV.

-- 
Bjørn






More information about the varnish-misc mailing list