We're currently using the s-maxage
directive in the Cache-Control header from our origin to control the TTL in Varnish. However, I'd like to remove it from the response before delivery, so that no other caches in the request chain act on it.
I'm currently looking at the header VMOD, to remove s-maxage
from the header, but leave the rest of it intact. I believe this could be achieved with something like this:
sub vcl_deliver {
header.regsub(resp, "s-maxage=[0-9]+,?\s?", "")
}
As a newcomer to Varnish, I wanted to sanity-check this approach and make sure there isn't a better way to tackle it?
Appreciate any support or advice.
Replace header at delivery time
The following VCL snippet will strip off the
s-maxage
attribute from theCache-Control
header before it is sent to the client.Replace header at storage time
It is also possible to strip off this attribute from the
Cache-Control
header before it gets stored into a cache object. In that case, you'll use theberesp.http.cache-control
variable insidevcl_backend_response
.Using vmod_headerplus
If you're using Varnish Enterprise, you can use the
vmod_headerplus
module to easily delete header attributes:Although Varnish Enterprise is the commercial version of Varnish Cache, you can still use it without upfront license payments if you use it on AWS, Azure or GCP.