Varnish Cache v4:不正确的后端健康检查响应
Varnish Cache v4: Incorrect Backend Health Check Response
我在我的 CMS 前设置了 Varnish Cache (4) 来帮助缓存请求。如果我的 CMS 出现故障,我想在设定的宽限期内交付缓存的项目。我遵循了许多在线提供的示例,但 运行 遇到了 Varnish 无法识别我的后端已关闭的问题。当我手动关闭 CMS 时,std.health(req.backend_hint))
继续 return true 并尝试从后端检索项目,然后 return 返回 503 响应。
问题:我是否错误地假设 std.health(req.backend_hint))
会识别出我的 CMS 已关闭?
这是我用来测试的 VCL 脚本:
sub vcl_recv {
# Initial State
set req.http.grace = "none";
set req.http.x-host = req.http.host;
set req.http.x-url = req.url;
return(hash);
}
sub vcl_backend_response {
set beresp.ttl = 10s;
set beresp.grace = 1h;
}
sub vcl_deliver {
# Copy grace to resp so we can tell from client
set resp.http.grace = req.http.grace;
# Add debugging headers to cache requests
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
}
else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_hit {
if (obj.ttl >= 0s) {
# normal hit
return (deliver);
}
# We have no fresh content, lets look at the cache
elsif (std.healthy(req.backend_hint)) {
# Backend is healthy. Limit age to 10s.
if (obj.ttl + 10s > 0s) {
set req.http.grace = "normal(limited)";
return (deliver);
} else {
# No candidate for grace. Fetch a fresh object.
return(fetch);
}
} else {
# backend is sick - use full grace
if (obj.ttl + obj.grace > 0s) {
set req.http.grace = "full";
return (deliver);
} else {
# no graced object.
return (fetch);
}
}
}
同样,当我关闭 CMS 时,std.healthy(req.backend_hint))
仍然报告后端正常并且从不跳转到最后的 else 语句。
感谢观看。
要正确使用 std.healthy
,您当然需要配置后端探测。因此,在 VCL 文件的顶部,您首先要配置一个探测器:
probe site_probe {
.request =
"HEAD / HTTP/1.1"
"Host: example.com"
"Connection: close";
.interval = 5s; # check the health of each backend every 5 seconds
.timeout = 3s; # timing out after 1 second by default.
.window = 5; # If 3 out of the last 5 polls succeeded the backend is considered healthy, otherwise it will be marked as sick
.threshold = 3;
}
确保将 example.com
替换为您的主要网站域名。放置(或省略)www.
前缀很重要,这样探测器就不会被重定向并标记为失败。
当然,您的后端定义应该配置为使用定义的探测器:
backend default {
.host = "127.0.0.1";
.port = "8080";
.probe = site_probe;
}
我在我的 CMS 前设置了 Varnish Cache (4) 来帮助缓存请求。如果我的 CMS 出现故障,我想在设定的宽限期内交付缓存的项目。我遵循了许多在线提供的示例,但 运行 遇到了 Varnish 无法识别我的后端已关闭的问题。当我手动关闭 CMS 时,std.health(req.backend_hint))
继续 return true 并尝试从后端检索项目,然后 return 返回 503 响应。
问题:我是否错误地假设 std.health(req.backend_hint))
会识别出我的 CMS 已关闭?
这是我用来测试的 VCL 脚本:
sub vcl_recv {
# Initial State
set req.http.grace = "none";
set req.http.x-host = req.http.host;
set req.http.x-url = req.url;
return(hash);
}
sub vcl_backend_response {
set beresp.ttl = 10s;
set beresp.grace = 1h;
}
sub vcl_deliver {
# Copy grace to resp so we can tell from client
set resp.http.grace = req.http.grace;
# Add debugging headers to cache requests
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
}
else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_hit {
if (obj.ttl >= 0s) {
# normal hit
return (deliver);
}
# We have no fresh content, lets look at the cache
elsif (std.healthy(req.backend_hint)) {
# Backend is healthy. Limit age to 10s.
if (obj.ttl + 10s > 0s) {
set req.http.grace = "normal(limited)";
return (deliver);
} else {
# No candidate for grace. Fetch a fresh object.
return(fetch);
}
} else {
# backend is sick - use full grace
if (obj.ttl + obj.grace > 0s) {
set req.http.grace = "full";
return (deliver);
} else {
# no graced object.
return (fetch);
}
}
}
同样,当我关闭 CMS 时,std.healthy(req.backend_hint))
仍然报告后端正常并且从不跳转到最后的 else 语句。
感谢观看。
要正确使用 std.healthy
,您当然需要配置后端探测。因此,在 VCL 文件的顶部,您首先要配置一个探测器:
probe site_probe {
.request =
"HEAD / HTTP/1.1"
"Host: example.com"
"Connection: close";
.interval = 5s; # check the health of each backend every 5 seconds
.timeout = 3s; # timing out after 1 second by default.
.window = 5; # If 3 out of the last 5 polls succeeded the backend is considered healthy, otherwise it will be marked as sick
.threshold = 3;
}
确保将 example.com
替换为您的主要网站域名。放置(或省略)www.
前缀很重要,这样探测器就不会被重定向并标记为失败。
当然,您的后端定义应该配置为使用定义的探测器:
backend default {
.host = "127.0.0.1";
.port = "8080";
.probe = site_probe;
}