首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让Varnish 4缓存所有内容,而不考虑缓存控制标头和cookie

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

Stack Overflow用户
提问于 2015-08-27 21:57:29
回答 1查看 7.2K关注 0票数 4

简而言之,我正在努力

代码语言:javascript
复制
   if (beresp.ttl < 120s) {
        set beresp.ttl = 120s;
        unset beresp.http.Cache-Control;
      }

在我的VCL配置文件中,它不工作。更多详细信息如下:

下面是我的请求头:

代码语言:javascript
复制
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并按enter,我相信。cookie是默认的php,一个通过session_start实现的cookie,也是一个自定义的会话cookie。

现在,我想忽略cookie和cache-control头。

下面是我的VCL设置:

代码语言:javascript
复制
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";
        }
}

响应头为:

代码语言:javascript
复制
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基本上只是回显一个随机数:

代码语言:javascript
复制
<?php

echo 'Hello worlds';

echo '<hr/>';

echo rand();

?>
EN

回答 1

Stack Overflow用户

发布于 2015-08-28 03:55:33

Cookie正在阻止缓存命中。尝试在vcl_recvvcl_backend_response中剥离cookies。

代码语言:javascript
复制
sub vcl_recv {
    unset req.http.cookie;
}

在vcl_backend_response中

代码语言:javascript
复制
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;
  }

}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32251378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档