无论缓存控制头和cookie如何,我如何使Varnish 4缓存所有内容

时间:2015-08-27 13:57:29

标签: php varnish varnish-vcl

简而言之,我正在尝试

   if (beresp.ttl < 120s) {
        set beresp.ttl = 120s;
        unset beresp.http.Cache-Control;
      }

在我的VCL配置文件中,它无法正常工作。更多细节如下:

这是我的请求标题:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:portal1 =dsfgsdfgsdfg; portal1 =sdfgsdfgsdfg; PHPSESSID=randomstring
Host:216.66.35.169
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36

请注意,默认情况下,只要有人输入网址和地址栏,Chrome就会设置max-age = 0的Cache-Control,然后点击输入我相信。 Cookie是默认的php,通过session_start和自定义会话cookie。

现在我想忽略cookie和缓存控制头。

这是我的VCL设置:

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
   # set beresp.ttl = 60s;


  if (beresp.ttl < 120s) {
    set beresp.ttl = 120s;
    unset beresp.http.Cache-Control;
  }
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.

    if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT";
        } else {
                set resp.http.X-Cache = "MISS";
        }
}

响应标题是:

Accept-Ranges:bytes
Age:0
Connection:keep-alive
Content-Length:24
Content-Type:text/html
Date:Thu, 27 Aug 2015 13:48:58 GMT
Server:Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8q PHP/5.3.5
Via:1.1 varnish-v4
X-Cache:MISS
X-Powered-By:PHP/5.3.5
X-Varnish:17

这是一个小姐。

后端服务器上的php基本上只是回显一个随机数:

<?php

echo 'Hello worlds';

echo '<hr/>';

echo rand();

?>

1 个答案:

答案 0 :(得分:3)

Cookie阻止缓存命中。请尝试在vcl_recvvcl_backend_response中删除Cookie。

sub vcl_recv {
    unset req.http.cookie;
}

并在vcl_backend_response

sub vcl_backend_response {


  if (beresp.ttl < 120s) {
      unset beresp.http.cookie;
      unset beresp.http.Set-Cookie;
      set beresp.ttl = 120s;
      unset beresp.http.Cache-Control;
  }

}