【问题标题】:Remove s-maxage from Cache-Control header using Varnish使用 Varnish 从 Cache-Control 标头中删除 s-maxage
【发布时间】:2021-02-02 15:59:52
【问题描述】:

我们目前在来自我们的源的 Cache-Control 标头中使用 s-maxage 指令来控制 Varnish 中的 TTL。但是,我想在交付之前将其从响应中删除,以便请求链中的其他缓存不会对其起作用。

我目前正在查看标头 VMOD,以从标头中删除 s-maxage,但保留其余部分不变。我相信这可以通过以下方式实现:

sub vcl_deliver {
    header.regsub(resp, "s-maxage=[0-9]+,?\s?", "")
}

作为 Varnish 的新手,我想彻底检查这种方法并确保没有更好的方法来解决它?

感谢任何支持或建议。

【问题讨论】:

    标签: varnish varnish-vcl varnish-4


    【解决方案1】:

    在交货时更换标题

    以下 VCL sn-p 将在发送到客户端之前从 Cache-Control 标头中去除 s-maxage 属性。

    sub vcl_deliver {
        set resp.http.cache-control = regsub(resp.http.cache-control,
    "(,\s*s-maxage=[0-9]+\s*$)|(\s*s-maxage=[0-9]+\s*,)","");
    }
    

    在存储时替换标题

    还可以在将Cache-Control 标头中的此属性存储到缓存对象之前将其剥离。在这种情况下,您将在vcl_backend_response 中使用beresp.http.cache-control 变量。

    sub vcl_backend_response {
        set beresp.http.cache-control = regsub(beresp.http.cache-control,
    "(,\s*s-maxage=[0-9]+\s*$)|(\s*s-maxage=[0-9]+\s*,)","");
    }
    

    使用 vmod_headerplus

    如果您使用的是Varnish Enterprise,您可以使用vmod_headerplus 模块轻松删除标题属性:

    vcl 4.1;
    
    import headerplus;
    
    sub vcl_deliver {
        headerplus.init(resp);
        headerplus.attr_delete("Cache-Control","s-maxage",",");
        headerplus.write();
    }
    
    vcl 4.1;
    
    import headerplus;
    
    sub vcl_backend_response {
        headerplus.init(beresp);
        headerplus.attr_delete("Cache-Control","s-maxage",",");
        headerplus.write();
    }
    

    虽然 Varnish EnterpriseVarnish Cache 的商业版本,但如果您在 AWS、Azure 或 GCP 上使用它,您仍然可以使用它而无需预先支付许可费用。 p>

    【讨论】:

    • 哇,Thijs,这是一个出色而全面的答案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多