Varnish 不缓存有会话的网站

Varnish 不缓存有会话的网站

我尝试过curl -I domainname并且这是响应。

HTTP/1.1 200 OK
Date: Tue, 21 Apr 2015 14:49:09 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=kpgqqefkge38jlqc608hq12046; path=/
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
Content-Type: text/html; charset=UTF-8
X-Varnish: 294982
Age: 0
Via: 1.1 varnish-v4
Content-Length: 0
Connection: keep-alive

HTTP/1.1 200 OK
Date: Tue, 21 Apr 2015 14:49:10 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=onqvu51sggp70nla7ke5scgr61; path=/
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
Content-Type: text/html; charset=UTF-8
X-Varnish: 163920
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive


HTTP/1.1 200 OK
Date: Tue, 21 Apr 2015 14:49:10 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=s8sd6qufhjl9ki0dh8jc2nb206; path=/
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
Content-Type: text/html; charset=UTF-8
X-Varnish: 294985
Age: 0
Via: 1.1 varnish-v4
Content-Length: 0
Connection: keep-alive

上述回应有以下观察:

  1. PHPsessionID 值在每次 HIT 时不断变化。

  2. Varnish Age 值保持为 0 。这意味着该网站没有被缓存?

我有一个需要使用会话的网站。因为用户可以登录,并且他们的详细信息保存在会话中。

有什么方法可以让网站被 Varnish 缓存吗?谢谢!

答案1

您必须禁用 PHP 的会话自动启动,并且仅在用户登录时启动会话。这样,PHP 就不会PHPSESSID为未登录的用户设置 cookie,从而让 Varnish 缓存这些页面。

相关内容