2

stale-if-error用来提供过时的内容,而grace服务器keepvcl_hit.

问题是:进入vcl子程序后是否可以传递缓存对象vcl_backend_error(新请求错误)。实际上,我提供了缓存对象,vcl_hit但查看下图,我看不出如何访问该请求的缓存对象。

https://www.varnish-software.com/book/4.0/chapters/VCL_Basics.html

资料来源:https ://www.varnish-software.com/book/4.0/chapters/VCL_Basics.html

4

1 回答 1

3

使用内置 VCL 时(参见下面的代码):

# Built-in 'vcl_hit'.
sub vcl_hit {
    if (obj.ttl >= 0s) {
        return (deliver);
    }

    if (obj.ttl + obj.grace > 0s) {
        return (deliver);
    }

    return (fetch);
}
  • 如果vcl_backend_error由 a 触发的后台/异步后端获取到达,return (deliver)vcl_hit无需担心。它只是一个更新停滞对象的后台线程。停滞的内容已经交付给客户。

  • 如果vcl_backend_error由 a 触发的同步后端获取到达,return (fetch)vcl_hit也不必担心。错误将传递给客户端,但您别无选择。Varnish 存储中没有停止的对象。

但是,如果您已自定义vcl_hit在后端健康时限制宽限(请参阅下面的 VCL 示例代码),则return (fetch)执行 duringvcl_hit将作为同步后端请求处理。客户端将等待后端响应。如果后端请求到达vcl_backend_error并且错误将被传递到客户端。Varnish 存储中有一个停止的对象(在本例中停止超过 60 秒),但不会使用它。

# Customised 'vcl_hit'.
sub vcl_hit {
    if (obj.ttl >= 0s) {
        return (deliver);
    }

    if (std.healthy(req.backend_hint)) {
        if (obj.ttl + 60s > 0s) {
            return (deliver);
        }
    } else {
        if (obj.ttl + obj.grace > 0s) {
            return (deliver);
        }
    }

    return (fetch);
}

如果您想在同步后端获取失败时交付停滞的对象,在这种情况下,您需要一些额外的 VCL 逻辑。这个想法显示在下面的代码中:

backend fail_be {
    .host = "127.0.0.1";
    .port = "9000";
    .probe = {
        .url = "/give-me-a-non-200-please";
        .interval = 24h;
        .timeout = 1s;
        .window = 1;
        .threshold = 1;
    }
}

sub vcl_recv {
    # Force the non-healthy backend in case of restart because of a previous
    # failed backend fetch. This will force serving stalled content using
    # full grace during 'vcl_hit' (if possible).
    if (req.restarts == 0) {
        unset req.http.X-Varnish-Restarted-5xx;
    } else {
        if (req.http.X-Varnish-Restarted-5xx) {
            set req.backend_hint = fail_be;
        }
    }

    # ...
}

sub vcl_synth {
    # 503 generated for synchronous client requests when abandoning the
    # backend request (see 'vcl_backend_fetch') and not executing a POST.
    if (resp.status == 503 &&
        req.method != "POST" &&
        !req.http.X-Varnish-Restarted-5xx) {
        set req.http.X-Varnish-Restarted-5xx = "1";
        return (restart);
    }

    # ...
}

sub vcl_backend_fetch {
    if (bereq.retries == 0) {
        unset bereq.http.X-Varnish-Backend-5xx;
    } else {
        if (bereq.http.X-Varnish-Backend-5xx) {
            # Jump to 'vcl_synth' with a 503 status code.
            return (abandon);
        }
    }

    # ...
}

sub vcl_backend_response {
    if (beresp.status >= 500 && beresp.status < 600) {
        set bereq.http.X-Varnish-Backend-5xx = "1";
        return (retry);
    }

    # ...
}

sub vcl_backend_error {
    set bereq.http.X-Varnish-Backend-5xx = "1";
    return (retry);
}
于 2015-10-06T12:55:27.920 回答