我如何更改 varnish 503 错误?
我如何自定义它?
我正在使用 varnish v 4
现在开始工作
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);
}
答案1
Varnish 4 中有两种错误。一种是后端获取错误。vcl_backend_error
处理此类错误。另一种是 VCL 中生成的错误。vcl_synth
处理此类错误。
就您而言,您正在定制vcl_error
子程序,这不是针对后端错误的。
您可以从以下 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);
}
确认错误信息
$ 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
答案2
我想提出一个替代方案...请参阅下面的示例 default.vcl 文件
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);
}
}
然后您可以将自定义 html 保存在 error503.html 中