问题描述
我们目前正在使用源头的 Cache-Control 标头中的 s-maxage
指令来控制 Varnish 中的 TTL。但是,我想在传递之前将其从响应中删除,以便请求链中的其他缓存不会对其进行操作。
我目前正在查看标题 VMOD,以从标题中删除 s-maxage
,但保留其余部分完整无缺。我相信这可以通过这样的方式实现:
sub vcl_deliver {
header.regsub(resp,"s-maxage=[0-9]+,?\s?","")
}
作为 Varnish 的新手,我想对这种方法进行理智检查并确保没有更好的方法来解决它?
感谢任何支持或建议。
解决方法
在交货时更换标题
以下 VCL 代码段将在发送到客户端之前从 s-maxage
标头中去除 Cache-Control
属性。
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
标头中去除这个属性。在这种情况下,您将在 beresp.http.cache-control
中使用 vcl_backend_response
变量。
sub vcl_backend_response {
set beresp.http.cache-control = regsub(beresp.http.cache-control,"");
}
使用 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",");
headerplus.write();
}
尽管 Varnish Enterprise 是 Varnish Cache 的商业版本,但如果您在 AWS、Azure 或 GCP 上使用它,您仍然可以使用它而无需预付许可费用。 >