Raphaël Malié
Raphaël Malié

Reputation: 4012

Varnish 4 does not honor Cache-Control: must-revalidate

I'm trying to make Varnish work with last-modified headers, but no matter what I do my page is cached during 120s, and Varnish never revalidate with the backend.

My backend is sending these headers :

Cache-Control: must-revalidate, proxy-revalidate, public, stale-while-revalidate=0
Last-Modified: Fri, 22 Jan 2016 03:32:33 GMT

And when I log the TTL of the object on hit, it's value is always set to 120s.

I am using the default VCL config of Varnish 4.

Regards,


Edit: After some search, I found that 120s is the default ttl value of Varnish. But why is he ignoring last-modified?

Upvotes: 2

Views: 1853

Answers (2)

Raphaël Malié
Raphaël Malié

Reputation: 4012

I got an answer from the Varnish mailing list, in order to emulate the "must-revalidate" header, this piece of VCL must be added:

sub vcl_backend_response {
    if (beresp.http.cache-control ~ "must-revalidate") {
        set beresp.ttl = 1s;
        set beresp.grace = 0s;
        set beresp.keep = 1w;
    }
}

It only works on Varnish 4.

I quote the reason for the 1s ttl :

This way, you'd only cache for 1 second (don't set it to 0, or all the requests for this object would be done sequentially), but will keep the object for a week, revalidating it each time it is requested and its ttl is expired.

Upvotes: 1

Ronald
Ronald

Reputation: 2932

Set the "s-maxage" or "max-age" attributes of the Cache-Control header:

beresp.ttl is initialized with the first value it finds among:

The s-maxage variable in the Cache-Control response header field
The max-age variable in the Cache-Control response header field
The Expires response header field
The default_ttl parameter.

See: http://book.varnish-software.com/4.0/chapters/VCL_Built_in_Subroutines.html#the-initial-value-of-beresp-ttl

Upvotes: 1

Related Questions