如何让Varnish 4缓存所有内容,无论缓存控制头和Cookie如何?

4
简而言之,我正在尝试
   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

请注意,当某人在地址栏中输入URL并按下回车键时,Chrome默认将Cache-Control设置为最大年龄(max-age)= 0。 Cookie是通过session_start设置的默认php cookie和自定义会话cookie。
目前我想忽略Cookie和Cache-Control头。
以下是我的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个回答

5

Cookies会阻止缓存击中。尝试在 vcl_recvvcl_backend_response 中删除Cookies。

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

}

已测试并按预期工作。无论头部中的cookie和缓存控制如何,都会进行缓存。 - Tyler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接