Search code examples
proxyvarnishvarnish-vcl

How to check if backend is healthy in vcl_recv


I have pretty sophisticated varnish config. I can't really use the directors and doing the routes manually.

//webservice1 and webservice2 has probes working there

set req.backend = webservice1;
if (req.backend.healthy)
{ 
     #redirect there 
}

set req.backend = webservice2;
if(req.backend.healthy)
{ 
     #change parameters with regex and redirect
}

This works. But looks really lame.

Is there any "legal" way to find out if backend is healthy? Like this:

if(webservice2.healthy)
{ 
     #change parameters with regex and redirect
}

This is not working, obviously.


Solution

  • I've been doing a lot of research about this and that's the only way to accomplish it (unless you want to write a vmod).

    Assuming you always want to respond with webservice1 unless it's sick, I may suggest a little refactoring:

    set req.backend = webservice1;
    if(!req.backend.healthy) {
         set req.backend = webservice2;
         #change parameters with regex
    }
    # redirect here