开发者

Varnish Cache not caching PHP pages when session_start() is used

I'm working with Varnish Cache for the 1st time and not having any joy getting it to work as I'd like.

My problem seems simple enough.

All I want it for .php pages to be cached.

So far every .php request always passes the varnish cache and hits my apache web server.

The problem is with cookies and the call to session_start();

No session_start() = caching With session_start() = no caching

Now the documentation is simple enough to understand but I've tried and tried unsuccessfully. As soon as I add a call to session_start() = no more caching.

Here is the sample HTML w/ PHP:

<?php 
session_start(); 
?>

<html>
<head>
</head>
<body>  

<?php echo date('Y-m-d H-i-s'); ?>

</body>
</html>

Here is my very clean and simple varnish default.vcl

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
  unset req.http.Cookie;
  return (lookup);
}

sub vcl_fetch {
    if (beresp.http.Set-Cookie) {
        unset beresp.http.Set-Cookie;
        return (deliver);
    }
}

This should unset the Cookie from client and server and it does appear to do so. When debugging with FireBug in FireFox I can see the Set-Cookie and Cookie HTTP headers are not there when I use this default.vcl

Here are the HTTP headers from a FireFox:

Response Headers:

HTTP/1.1 200 OK
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Type: text/html
Content-Length: 192
Date: Wed, 23 Feb 2011 01:34:17 GMT
X-Varnish: 2052563421
Age: 0
Via: 1.1 varnish
Connection: keep-alive

Request Headers:

GET /test.php HTTP/1.1
Host: xshare.com.local
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-al开发者_开发百科ive
Cache-Control: max-age=0

I just can't see where I'm going wrong. I have tinkered for 2 hours, adding/altering and removing lines and variants to the .vcl configuration file.

Can anyone offer any advice?

Many Thanks.


As you probably found out by now, caching and cookies don't match. You're removing your session cookie (and all others) in the example above, so you won't have your session available when using Varnish this way.

2 things you can do: - allow the cookie but get no caching for logged in users - don't use a session (or cookies) unless you really have to

A 3rd more complex solution is to strip cookies for most pages, but allow them for some pages which you then handle as ESI blocks. Lots of documentation exists about that.


I've found a combination of rules that now is caching .php pages with session_start() in them.

I've left in but commented out some of those that I've tried along the way.

Trial and error now has it down to just 2 config rules.

One for the client and one for the server.

Client = unset req.http.Cookie;

Server = set beresp.ttl = 5d;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
  unset req.http.Cookie;
#  unset req.http.Cache-Control;
  return (lookup); 
}

sub vcl_fetch {
     if (beresp.http.Set-Cookie) {
         set beresp.ttl = 5d;
#         unset beresp.http.Set-Cookie;
#         unset beresp.http.Expires;
#         unset beresp.http.Cache-Control;
         return (deliver);
     }
}

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜