Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible for Varnish to redirect traffic to different IPs based on the url?

For example, is the following setup feasible (and if so, what would the VCL look like):

  1. * points to Varnish IP address
  2. When a request is made to, varnish checks the cache and sends the request to Server1's IP address on a cache miss.
  3. When a request is made to, varnish checks the cache and sends the request to Server2's IP address on a cache miss.

foo and bar are (for the most part) completely unrelated sites. They use the engine, but have different content and their own distinct database. Since there previously was no penalty for doing so (other than cost) we split them up into two separate boxes so that a ton of traffic to foo won't have a negative impact on visitors browsing around bar.

I could set up two instances of varnish and have one serve up foo's static content and the other serve up bar's, but as there doesn't seem to be much overhead to running Varnish, I think (perhaps mistakenly) that it would make more sense to go with one Varnish server that redirects the traffic to the appropriate box on a cache miss.

share|improve this question
up vote 4 down vote accepted

Yes, it is. Try defining in default.vcl your backends first:

backend foo {
  .host = ""; # IP of foo backend
  .port = "80";

backend bar {
  .host = ""; # IP of bar backend
  .port = "80";

and use in vcl_recv() code similar to:

 if ( ~ "") {
        set req.backend = foo;
        set req.backend = bar;

There is good documentation (good enough for such examples ;)) on and You can do much more with VCL. It's realy cool.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.