如何让 Varnish 4 缓存所有内容,而不管缓存控制标头和 cookie

Har*_*ker 4 php varnish varnish-vcl

简而言之,我正在尝试

   if (beresp.ttl < 120s) {
        set beresp.ttl = 120s;
        unset beresp.http.Cache-Control;
      }
Run Code Online (Sandbox Code Playgroud)

在我的 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
Run Code Online (Sandbox Code Playgroud)

请注意,每当有人输入 url 和地址栏中并点击 enter 我相信时,Chrome 默认设置 max-age=0 的 Cache-Control 。cookie 是通过 session_start 的默认 php cookie,也是自定义会话 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";
        }
}
Run Code Online (Sandbox Code Playgroud)

响应头是:

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
Run Code Online (Sandbox Code Playgroud)

这是一个错过。

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

<?php

echo 'Hello worlds';

echo '<hr/>';

echo rand();

?>
Run Code Online (Sandbox Code Playgroud)

qux*_*qux 5

Cookie 可以防止缓存命中。尝试在vcl_recv和 中剥离 cookie vcl_backend_response

sub vcl_recv {
    unset req.http.cookie;
}
Run Code Online (Sandbox Code Playgroud)

在 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;
  }

}
Run Code Online (Sandbox Code Playgroud)