7

How can i change varnish 503 error?
How can i customize it ?
i'm using varnish v 4

it's now work

sub vcl_synth {
    set resp.http.Content-Type = "text/html; charset=utf-8";
    set resp.http.Retry-After = "5";
    synthetic( {"<!DOCTYPE html>
<html>
  <head>
    <title>Under Maintenance</title>
  </head>
  <body>
    <h1>Under Maintenance</h1>
    <p></p>
    <hr>
  </body>
</html>
"} );
    return (deliver);
}
4

There are two kinds of errors in Varnish 4. One is backend fetch errors.vcl_backend_error handles this kind of errors. Another is errors generated in VCL. vcl_synth handles this kind of errors.

In your case, you're customizing vcl_error subroutine, that is not for backend errors.

You can distinguish these two kinds of erros from following default.vcl.

vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (req.url ~ "^/404") {
        return (synth(999, "make 404 error explicitly"));
    }
}

sub vcl_backend_response {
}

sub vcl_deliver {
}

sub vcl_backend_error {
    set beresp.http.Content-Type = "text/html; charset=utf-8";
    synthetic( {"errors due to backend fetch"} );
    return (deliver);
}

sub vcl_synth {
    if (resp.status == 999) {
        set resp.status = 404;
        set resp.http.Content-Type = "text/plain; charset=utf-8";
        synthetic({"errors due to vcl"});
        return (deliver);
    }
    return (deliver);
}

Confirm error messages

$ curl http://localhost:6081/   # If the backend server is not running, "503 Backend fetch failed" error occurs 
errors due to backend fetch
$ curl http://localhost:6081/404/foo
errors due to vcl
11

I would like to propose an alternative... please find below a sample default.vcl file

vcl 4.0;

import std;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_backend_response {
       if (beresp.status == 503 && bereq.retries < 5 ) {
       return(retry);
 }
}

sub vcl_backend_error {
      if (beresp.status == 503 && bereq.retries == 5) {
          synthetic(std.fileread("/etc/varnish/error503.html"));
          return(deliver);
       }
 }

sub vcl_synth {
    if (resp.status == 503) {
        synthetic(std.fileread("/etc/varnish/error503.html"));
        return(deliver);
     }
}

sub vcl_deliver {
    if (resp.status == 503) {
        return(restart);
    }
  }

And then you can save your custom html in error503.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.