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

Yann Malet yann.malet at gmail.com
Tue Aug 18 16:41:16 CEST 2009


Hello Rob,

Thank you for this detailed solution I have tested it and it work great. I
have created a file called migration.vcl that contains:

"""
backend oldcmsnode { .host = "old_cms_webserver"; .port="8081"; }
backend newcmsnode { .host = "new_cms_webserver"; .port="8082"; }
director oldcms random {
   { .backend = oldcmsnode ; .weight = 1; }
}
director newcms random {
   { .backend = newcmsnode ; .weight = 1; }
}
sub vcl_recv {
    set req.backend = newcms;
    if (req.restarts > 0) {
       set req.backend = oldcms;
    }
}
sub vcl_fetch {
    if (obj.status == 404 && req.restarts==0) {
       restart;
    }
}
"""

Then I run it using this command :
sudo varnishd -a 127.0.0.1:8080 -F -f /etc/varnish/migration.vcl
Regards,
Yann


On Tue, Aug 18, 2009 at 3:19 AM, Rob S <rtshilston at gmail.com> wrote:

> 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
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20090818/ae04507c/attachment-0001.html>


More information about the varnish-misc mailing list