2

How can I customize the '503 backend fetch failed' of Varnish 4?

I'm customizing the other errors when they get through synth stage:

sub vcl_synth {
    if (resp.status == 750) {
        set resp.http.location = "http://mobile.cronica.com.ar/";
        set resp.status = 302;
        set resp.http.Action = "Redirect";  
        return(deliver);
    }
    if (resp.status == 751) {
        set resp.status = 301;
        set resp.http.location = "http://www." + req.http.host + req.url;
        set resp.http.Action = "Redirect";
        return(deliver);
    }
    else {
         synthetic( {"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>"} + resp.status + " " + resp.reason + {"</title>
    <link href='http://fonts.googleapis.com/css?family=Oswald:400,700' rel='stylesheet' type='text/css'>

  </head>
  <body style="background-color:#444; font-family: 'Oswald', sans-serif;">
    <h1 style="color:#DD8363;">Error "} + resp.status + " " + {"</h1>
    <p style="color:#5F88C4; ">"} + resp.reason + {"</p>
    <h3 style="color:white;">CEPI Says</h3>
    <p style="color:#bdb76b;">XID: "} + req.xid + {"</p>
    <p style="color:#bdb76b;">Edge-Server: "} + server.hostname + {"</p>
    <hr>
    <p style="color:#65b042;">2.0</p>
  </body>
</html>
"} ); 
        return(deliver);
    }

}

but the 503 error seems to avoid that.

nlopez
  • 528
  • 1
  • 6
  • 14

1 Answers1

3

According to Varnish Processing States control after backend_error() should be passed to vcl_synth(), but in reality the error page you are seeing is delivered unconditionally in backend_error() of builtin.vcl. Either you customize your webpage there or you add sub vcl_backend_error { return(retry); } in your vcl to force the jump to vcl_synth(), which will be with resp.status = 503.

Jorge Nerín
  • 766
  • 6
  • 8
  • Great! that's the solution. One more question, is there any way to put the server.hostname (the edge server hostname) on the error message in this subrutine (vcl_backend_error)? – nlopez Jan 30 '15 at 13:24
  • I'm not sure about what hostname you want, `server.hostname` is available in `vcl_synth` (that's the one it received the client request) and `beresp.backend.name`, `beresp.backend.ip` are available in `vcl_backend_error` (the backend that generated the error or "(null)" if there is no available backend). In my test with `new vdir = directors.round_robin();` and no alive backends `beresp.backend.name + beresp.backend.ip` → `vdir (null)` – Jorge Nerín Jan 30 '15 at 14:01