清漆缓存v4:不正确的后端运行状况检查响应

时间:2018-03-29 15:20:43

标签: varnish varnish-vcl varnish-4

我在CMS前面设置了Varnish Cache(4)以帮助缓存请求。如果我的CMS发生故障,我希望在设定的宽限期内提供缓存的项目。我已经关注了许多在线提供的示例,但我遇到的问题是Varnish不承认我的后端已关闭。当我手动关闭CMS时,std.health(req.backend_hint))继续返回true并尝试从后端检索项目,然后返回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))仍会将后端报告为健康,并且永远不会跳转到最终的其他声明。

谢谢你看看。

1 个答案:

答案 0 :(得分:1)

要正确使用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;
}