Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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. *.example.com points to Varnish IP address
  2. When a request is made to foo.example.com, varnish checks the cache and sends the request to Server1's IP address on a cache miss.
  3. When a request is made to bar.example.com, 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
add comment

1 Answer

up vote 4 down vote accepted

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

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

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

and use in vcl_recv() code similar to:

 if (req.http.host ~ "foo.example.com") {
        set req.backend = foo;
 }else{
        set req.backend = bar;
 }

There is good documentation (good enough for such examples ;)) on http://www.varnish-cache.org/docs/2.1/ and You can do much more with VCL. It's realy cool.

share|improve this answer
add comment

Your Answer

 
discard

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.