Proxy from and ordered list of web server until one of them send a 200 status code

Rob S rtshilston at gmail.com
Tue Aug 18 09:19:36 CEST 2009


Yann Malet wrote:
> Browser request the page :  *frontend:8080/foo/bar*
> This request reach the frontend:8080, it looks if the page is the 
> cache. If the page is in the cache it serves it from there else it 
> sends the request to* weserver_new_cms:8081*. There are 2 cases there 
> the page exists or  not. If the page exists it serves the page to the 
> frontend that puts it in the cache and sends it to the client. If the 
> page does not exist it means that weserver_new_cms:8081 returns 404 
> the frontend should reverse proxy *to webserver_old_cms:8082. *There 
> is  again 2 cases there the page exists or it doesn't. If the page 
> exists it serves the page to the frontend that puts it in the cache 
> and send it to the client. If the page does not exist it returns a 404 
> error to the client because the page does not exist in any (new, old) cms.
>
> It seems to me that *vcl_fetch* is the right place to hook this logic 
> but so far I have no idea on how to write this. Some help/ guidance 
> would be very appreciated.
>
Yann,

Varnish can definitely do this, and by default Varnish will serve from 
its cache anything that is there.  So, you just need to worry about the 
"it's not in the cache" scenario, and instead do something like the 
following.  First, you'll need to define your backend nodes:

   backend oldcmsnode { .host = "webserver_old_cms"; .port="8082"; }
   backend newcmsnode { .host = "webserver_new_cms"; .port="8081"; }

   director oldcms random {
      { .backend = oldcmsnode ; .weight = 1; }
   }

   director newcms random {
      { .backend = newcmsnode ; .weight = 1; }
   }

then, at the top of sub vcl_recv, we say "If we're trying for the first 
time, use the newcmsnode, otherwise use the oldcmsnode"

   set req.backend = newcmsnode;
   if (req.restarts > 0) {
      set req.backend = oldcmsnode;
   }

in vcl_fetch, put some logic to say "if we got a 404, and it was our 
first attempt (and therefore we're using the newcmsnode), we should 
restart and try again".

   if (obj.status == 404 && req.restarts==0) {
      restart;
   }

I hope this points you in the right direction.



Rob




More information about the varnish-misc mailing list