Search code examples
cachingload-balancingvarnishvarnish-vclvarnish-4

Varnish emergency backend or serve static error


My idea is to configure Varnish-cache on primary backend fails (HTTP 503 for example), first of all to try another backend, if failed, serve static error message.

Is it possible to configure it that way? P.S. I don't want varnish to work with emergency backend unless primary has really failed. Emergency backend always has a bit outdated data.

I am using Varnish 4, planing to move to 5.X soon. Backend is Java or PHP applications.


Solution

  • Sure you can do that, you should change your vcl_backend_response code tuning it with bereq.retries and return(retry) :

    sub vcl_backend_response {
            if ( beresp.status == 503 && bereq.retries == 0 ) {
                    set bereq.http.Host = "myNewHost";
                    return(retry);
            }
            if ( beresp.status == 503 && bereq.retries > 0 ) {
                    return (synth(503, "Oh noes!"));
            }
    
    }