为什么 Varnish 不能正确缓存我的索引页?

为什么 Varnish 不能正确缓存我的索引页?

我在 Ubuntu 11.10 上的 Apache 2 前面设置了一个 Varnish。我正在使用这个 VCL 文件:

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

sub vcl_recv {

  if (req.url ~ "^/web") {
    unset req.http.cookie;
  }

  if (req.url ~ "administration" ||
    req.url ~ "preview"
  ) {
    return(pass);
  }

  if (req.http.Cookie) {
    set req.http.Cookie = ";"+req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(mag_header)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
      remove req.http.Cookie;
    }
  }

  if (req.url ~ "\.(html|gif|jpg|jpeg|png|js|css)$") {
    unset req.http.cookie;
    return(lookup);
  }

  if (req.http.x-forwarded-for) {
    set req.http.X-Forwarded-For =
    req.http.X-Forwarded-For + ", " + client.ip;
  }
  else {
    set req.http.X-Forwarded-For = client.ip;
  }
  if (req.http.x-forwarded-host) {
    set req.http.X-Forwarded-Host =
    req.http.X-Forwarded-Host + ", " + server.ip;
  }
  else {
    set req.http.X-Forwarded-Host = server.ip;
  }
  return(lookup);
}

sub vcl_hash {
  hash_data(req.http.cookie);
}

sub vcl_fetch {
  set beresp.ttl =30m;
}

mag_header cookie 的值只能是三个值中的一个,从而将缓存的变体数量降到最低。不过,我看到主页上有很多未命中的情况(大约每 60 秒 7 次),而这些应该缓存 30 分钟。

知道为什么它不缓存吗?

欢呼吧,马克

编辑

下面是对主页发出的请求(不幸的是,我不确定这是否是我的问题的一个很好的例子,如果我应该抓取另一个,请告诉我):

29 RxHeader     c User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
29 RxHeader     c Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
29 RxHeader     c Referer: http://www.vmagazine.com/site/content/451/the-discreet-charm-of-kate-upton
29 RxHeader     c Accept-Encoding: gzip,deflate,sdch
29 RxHeader     c Accept-Language: en-US,en;q=0.8
29 RxHeader     c Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
29 RxHeader     c Cookie: vmag_header=vmag; __utma=232335640.704196202.1357394712.1357394712.1357394712.1; __utmb=232335640.25.10.1357394712; __utmc=232335640; __utmz=232335640.1357394712.1.1.utmcsr=popwatch.ew.com|utmccn=(referral)|utmcmd=referral|utmcct=/2013/01/04/krist
29 VCL_call     c recv lookup
29 VCL_call     c hash
29 Hash         c vmag_header=vmag
29 Hash         c /
29 Hash         c www.vmagazine.com
29 VCL_return   c hash
29 HitPass      c 1682448674
29 VCL_call     c pass pass
29 Backend      c 19 default default
29 TTL          c 1682448965 RFC 120 -1 -1 1357394808 0 1357394807 0 0
29 VCL_call     c fetch
29 TTL          c 1682448965 VCL 1800 -1 -1 1357394808 -0
29 TTL          c 1682448965 VCL 120 -1 -1 1357394808 -0
29 VCL_return   c hit_for_pass
29 ObjProtocol  c HTTP/1.1
29 ObjResponse  c OK
29 ObjHeader    c Date: Sat, 05 Jan 2013 14:06:47 GMT
29 ObjHeader    c Server: Apache/2.2.20 (Ubuntu)
29 ObjHeader    c X-Powered-By: PHP/5.3.6-13ubuntu3.9
29 ObjHeader    c Set-Cookie: vmag_header=vmag; path=/
29 ObjHeader    c Vary: Accept-Encoding
29 ObjHeader    c Content-Encoding: gzip
29 ObjHeader    c Content-Length: 16315
29 ObjHeader    c Content-Type: text/html
29 ObjHeader    c X-Pad: avoid browser bug
29 Gzip         c u F - 16315 77445 80 80 130452
29 VCL_call     c deliver deliver
29 TxProtocol   c HTTP/1.1
29 TxStatus     c 200
29 TxResponse   c OK
29 TxHeader     c Server: Apache/2.2.20 (Ubuntu)
29 TxHeader     c X-Powered-By: PHP/5.3.6-13ubuntu3.9
29 TxHeader     c Set-Cookie: vmag_header=vmag; path=/
29 TxHeader     c Vary: Accept-Encoding
29 TxHeader     c Content-Encoding: gzip
29 TxHeader     c Content-Type: text/html
29 TxHeader     c X-Pad: avoid browser bug
29 TxHeader     c Content-Length: 16315
29 TxHeader     c Accept-Ranges: bytes
29 TxHeader     c Date: Sat, 05 Jan 2013 14:06:47 GMT
29 TxHeader     c X-Varnish: 1682448965
29 TxHeader     c Age: 0
29 TxHeader     c Via: 1.1 varnish
29 TxHeader     c Connection: keep-alive
29 Length       c 16315
29 ReqEnd       c 1682448965 1357394807.567663431 1357394807.963178158 0.000065088 0.395375967 0.000138760

答案1

问题是我的代码中有一个 session_start() 调用,它添加了一个 PHP_SESSION cookie,这意味着缓存从未命中。

相关内容