【问题标题】:How do I make Varnish 4 cache everything regardless of cache-control headers and cookies无论缓存控制标头和cookie如何,我如何使Varnish 4缓存所有内容
【发布时间】:2015-11-21 22:53:56
【问题描述】:

简而言之,我正在尝试

   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 默认将 Cache-Control 设置为 max-age=0,只要有人输入 url 和地址栏中并点击我相信。 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";
        }
}

响应头是:

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();

?>

【问题讨论】:

    标签: php varnish varnish-vcl


    【解决方案1】:

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

    【讨论】:

    • 经过测试并按预期工作。缓存不管标头中的 cookie 和缓存控制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 2012-03-01
    • 2011-07-25
    • 2011-02-05
    • 2019-01-14
    • 2020-09-24
    相关资源
    最近更新 更多