如何配置 Varnish 以从缓存中排除特定文件夹?

如何配置 Varnish 以从缓存中排除特定文件夹?

如何设置 Varnish,使其不缓存特定文件夹(及其子文件夹)的内容,比如 /mnt/var/public_html/useroutput/

我尝试编辑 /etc/varnish/default.vcl

sub vcl_recv {
if (req.restarts == 0) {
  if (req.http.x-forwarded-for) {
      set req.http.X-Forwarded-For =
          req.http.X-Forwarded-For + ", " + client.ip;
  } else {
      set req.http.X-Forwarded-For = client.ip;
  }
}
if (req.request != "GET" &&
  req.request != "HEAD" &&
  req.request != "PUT" &&
  req.request != "POST" &&
  req.request != "TRACE" &&
  req.request != "OPTIONS" &&
  req.request != "DELETE") {
    /* Non-RFC2616 or CONNECT which is weird. */
    return (pipe);
}
    if (req.url ~ "^/useroutput") {
            return (pass);
    }
if (req.request != "GET" && req.request != "HEAD") {
    /* We only deal with GET and HEAD by default */
    return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
    /* Not cacheable by default */
    return (pass);
}
return (lookup);

}

但它不工作

请建议我如何修复它。

响应标头

    Accept-Ranges:bytes
Age:0
Cache-Control:max-age=172800
Connection:keep-alive
Content-Encoding:gzip
Content-Length:10274
Content-Type:text/html; charset=UTF-8
Date:Tue, 13 Sep 2016 11:15:19 GMT
Expires:Thu, 15 Sep 2016 11:15:19 GMT
Server:Apache/2.2.15 (CentOS)
Vary:Accept-Encoding,User-Agent
X-Cache:MISS
X-Powered-By:PHP/5.6.13
X-Varnish:909996209

请求标头

    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:_csrf=eb7266d5fd58a94f68f73514f2e2940c661222b810bcd214f190efc326cf1d51s%3A32%3A%22kMAh10CYLOhYVlVleXHkRX-aWAqdXytJ%22%3B; PHPSESSID=0tuegrs49ooa0alert6efcu3k7; wp_lead_uid=z3thLcIf6HBdK0GnJcp2c0ZHxfzWLMhUwvh; inbound_referral_site=Direct Traffic; style=null; lead_session=1; _gat=1; _identity=6a6fbb91546910ef2b03dd8c1a5a987a4a988c5915d7ae0c7275cf650ce31bd8s%3A47%3A%22%5B17%2C%22ekSOaz5sJC63xPKhSRTqaENTuc1E_BDh%22%2C1209600%5D%22%3B; _ga=GA1.2.478232635.1473741537
Host:xyz.com
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36

答案1

因为您在末尾有一个“查找”,并且它一直在缓存,所以似乎它不符合您的“通过”IF 条件。检查是否必须添加 * 或其他内容才能匹配“/useroutput/something/anotherthing”

在评论中,询问是否知道页面是否被缓存。您可以设置一个 cookie 来查看对象何时命中/未命中(我使用的是 Varnish 4)

sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT";
  } else {
    set resp.http.X-Cache = "MISS";
  }

  # Remove some headers: Nginx version & OS
  unset resp.http.Via;

  return (deliver);
}

相关内容