Change varnish 4 503 error
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
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