我第一次使用Varnish Cache,但是没有成功地让它按照我的意愿工作。
我的问题似乎很简单。
我想要缓存所有的 .php 页面。
到目前为止,每个 .php 请求都会经过 Varnish 缓存并且命中我的 Apache Web 服务器。
问题出在 cookies 和调用 session_start() 上。
没有 session_start() = 缓存生效 有 session_start() = 缓存失效
现在,文档很容易理解,但是我已经尝试了很多次,但都没有成功。只要我添加一个 session_start() 的调用,就再也没有缓存了。
这里是带有 PHP 的示例 HTML:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php echo date('Y-m-d H-i-s'); ?>
</body>
</html>
这是我的非常简洁干净的Varnish默认.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);
}
}
这应该从客户端和服务器上取消Cookie,看起来确实如此。在使用此default.vcl进行调试时,使用FireFox中的FireBug可以看到Set-Cookie和Cookie HTTP标头不存在。
以下是FireFox的HTTP标头:
响应标头:
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-alive
Cache-Control: max-age=0
我就是看不出哪里出了问题。 我已经在.vcl配置文件中调整了2个小时,添加/修改和删除了代码行和变量。
有人能提供一些建议吗?
非常感谢。