Using varnish as a cache for more than one website

Marcus Smith marcussmith at
Tue Jun 29 12:23:22 CEST 2010

Davies Matt J A (LCSS) wrote:
> Hello everyone
> What is the best practice way of setting up varnish to act as a cache
> for more than one web site?
> I can see I can make multiple backend, and I've read that I can run
> multiple instances of varnish, I'm just wondering what is the best
> practice way?
> One thing I can't quite get my head around is how do I tell varnish
> which incoming URL to serve with which backend, or instance.
> Scenario
> I've got our DNS boys to point and at our varnish server,
> which is going to act as a cache for both sites, both sites being
> very different and needing different rules.  Rules that may collide
> also.
> If I use the multiple instance route, how do associate an instance
> with an incoming URL?

With a single instance of varnish, having defined your back ends, you
might for example do something like this:

sub vcl_recv {
	if ( == "") {
		set req.backend = backend-A;
		# More site-specific rules here

	elsif ( ~ "(www\.)?b\.com") {
		set req.backend = backend-B;
		# More site-specific rules here

	# More sites here

	else {
		error 404 "Unknown host!";

best wishes,

More information about the varnish-misc mailing list