【问题标题】:Varnish expires header configurationVarnish 过期标头配置
【发布时间】:2017-09-11 18:32:15
【问题描述】:

请帮助我在清漆配置中添加过期标头。 vcl_fetch中已经定义了max_age,需要根据max_age添加expires header。

【问题讨论】:

    标签: varnish varnish-vcl expires-header


    【解决方案1】:

    通常除了Cache-Control 之外,您不需要设置Expires 标头。 Expires 标头告诉缓存(无论是代理服务器还是浏览器缓存)缓存文件,直到达到Expires 时间。如果同时定义了Cache-ControlExpires,则Cache-Control 优先。

    考虑以下响应标头:

    HTTP/1.1 200 OK
    Content-Type: image/jpeg
    Date: Fri, 14 Mar 2014 08:34:00 GMT
    Expires: Fri, 14 Mar 2014 08:35:00 GMT
    Cache-Control: public, max-age=600
    

    根据Expires 标头,内容应在一分钟后刷新,但由于 max-age 设置为 600 秒,因此图像会一直缓存到格林威治标准时间 08:44:00。

    如果您希望在特定时间使内容过期,则应删除 Cache-Control 标头并仅使用 Expires

    Mark Nottingham 写了一个很好的tutorial on caching。在考虑您的缓存策略时,绝对值得一读。

    如果您希望基于Cache-Control: max-age 设置Expires 标头,则需要在您的VCL 中使用inline-C。以下内容是从https://www.varnish-cache.org/trac/wiki/VCLExampleSetExpires 复制的,以防将来删除该页面。

    添加以下原型:

    C{
            #include <string.h>
            #include <stdlib.h>
    
            void TIM_format(double t, char *p);
            double TIM_real(void);
    }C
    

    以及 vcl_deliver 函数的以下内联 C:

    C{
            char *cache = VRT_GetHdr(sp, HDR_RESP, "\016cache-control:");
            char date[40];
            int max_age = -1;
            int want_equals = 0;
            if(cache) {
                    while(*cache != '\0') {
                            if (want_equals && *cache == '=') {
                                    cache++;
                                    max_age = strtoul(cache, 0, 0);
                                    break;
                            }
    
                            if (*cache == 'm' && !memcmp(cache, "max-age", 7)) {
                                    cache += 7;
                                    want_equals = 1;
                                    continue;
                            }
                            cache++;
                    }
                    if (max_age != -1) {
                            TIM_format(TIM_real() + max_age, date);
                            VRT_SetHdr(sp, HDR_RESP, "\010Expires:", date, vrt_magic_string_end);
                    }
            }
    }C
    

    【讨论】:

      【解决方案2】:

      假设 max-age 已经设置(即通过您的网络服务器),您可以在您的 vcl 中使用此配置设置 Expires 标头:

      # Add required lib to use durations
      import std;
      
      sub vcl_backend_response {
      
          # If max-age is setted, add a custom header to delegate calculation to vcl_deliver
          if (beresp.ttl > 0s) {
              set beresp.http.x-obj-ttl = beresp.ttl + "s";
          }
      
      }
      
      sub vcl_deliver {
      
          # Calculate duration and set Expires header
          if (resp.http.x-obj-ttl) {
              set resp.http.Expires = "" + (now + std.duration(resp.http.x-obj-ttl, 3600s));
              unset resp.http.x-obj-ttl;
          }
      }
      

      来源:https://www.g-loaded.eu/2016/11/25/how-to-set-the-expires-header-correctly-in-varnish/

      附加信息:您可以使用此示例在您的 apache 服务器上设置 max-age

      <LocationMatch "/(path1|path2)/">
          ExpiresActive On
          ExpiresDefault "access plus 1 week"
      </LocationMatch>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多