Lukasz Kujawa
Lukasz Kujawa

Reputation: 3096

Varnish doesn't cache PHP session files (beresp.ttl = -1)

I'm unable to make Varnish 3.0.2 cache files generated by PHP where session is used. I'm aware that by default Varnish won't cache files with Set-Cookie but I believe I'm removing this header.

sub vcl_recv {
        # PHP Generated CSS
        if( req.url ~ "^/css/" ) {
                unset req.http.Max-Age;
                unset req.http.Pragma;
                unset req.http.Cache-Control;
                unset req.http.Cookie;
                return(lookup);
        }

sub vcl_fetch {
 if( req.url ~ "^/css/" ) {
     remove beresp.http.Cache-Control;
     remove beresp.http.Pragma;
     remove beresp.http.set-cookie;
 }

 if (beresp.ttl <= 0s ||
     beresp.http.Set-Cookie ||
     beresp.http.Vary == "*") {
            std.log("--------- HIT FOR PASS --------");
            set beresp.ttl = 920s;
            return (hit_for_pass);
 }
 return (deliver);
}

Every PHP request will go to hit_for_pass and never get cached. TTL value is always -1.

Upvotes: 0

Views: 630

Answers (1)

lkarsten
lkarsten

Reputation: 2981

From the information that is available here, the most probable reason is that the backend sends a Cache-Control response header that makes Varnish set the TTL to 0s.

Look for the "TTL" log lines in varnishlog, like these:

   21 TTL          c 216230930 RFC 600 -1 -1 1362839670 0 1362839669 1362840269 600
   21 TTL          c 216230930 VCL 600 86400 -1 1362839670 -0

The first record is what Varnish (before vcl_fetch runs) decided the response headers meant for the TTL, the second is what it was after some modification in VCL. The order of the first three is TTL, grace and keep. You only need to worry about the the TTL. which in this case is 600s/10 minutes.

The use of return() in both recv and fetch here isn't necessary. Just let the logic fall through to the default VCL instead, it will save you pain in the long run.

Upvotes: 1

Related Questions