1

我在 centos 7 上运行了一个清漆。我的网站在 Drupal 上运行。Varnish 可以完美运行,但浏览器也会缓存内容,这会影响用户看到的内容。

将帖子添加到站点清漆时会重置缓存,但浏览器缓存会显示旧的缓存数据。

如何在浏览器上禁用缓存以使其影响清漆。

我的默认.vcl:

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.
# 
backend default {
  .host = "mysite.com";
  .port = "6060";
  .connect_timeout = 3600s;
  .first_byte_timeout = 3600s;
  .between_bytes_timeout = 3600s;
}

# 
# Below is a commented-out copy of the default VCL logic.  If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
# 
 sub vcl_recv {
 
	  # Add a unique header containing the client address
		remove req.http.X-Forwarded-For;
		set    req.http.X-Forwarded-For = client.ip;
 
	  # set the custom header
	  if (req.http.Cookie ~ "ABtesting=B") {
		set req.http.X-ABtesting = "B";
	  } else {
		set req.http.X-ABtesting = "A";
	  }
 
	# do this only once per request
	if (req.restarts == 0) {
	  # normalize Accept-Encoding to reduce vary
	  if (req.http.Accept-Encoding) {
		if (req.http.User-Agent ~ "MSIE 6") {
		  unset req.http.Accept-Encoding;
		} elsif (req.http.Accept-Encoding ~ "gzip") {
		  set req.http.Accept-Encoding = "gzip";
		} elsif (req.http.Accept-Encoding ~ "deflate") {
		  set req.http.Accept-Encoding = "deflate";
		} else {
		  unset req.http.Accept-Encoding;
		}
	  }
	}
	
	if (req.http.User-Agent ~ "(Mobile|Android|iPhone|iPad)") {
	  set req.http.User-Agent = "mobile";
	} else {
	  set req.http.User-Agent = "desktop";
	}
	
	
     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.request != "GET" && req.request != "HEAD") {
         /* We only deal with GET and HEAD by default */
         return (pass);
     }
	// Remove has_js and Google Analytics __* cookies.
	set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
	// Remove a ";" prefix, if present.
	set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
     if (req.http.Authorization /*|| req.http.Cookie*/) {
         /* Not cacheable by default */
         return (pass);
     }
     return (lookup);
 }

# 
# sub vcl_pipe {
#     # Note that only the first request to the backend will have
#     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
#     # have it set for all requests, make sure to have:
#     # set bereq.http.connection = "close";
#     # here.  It is not set by default as it might break some broken web
#     # applications, like IIS with NTLM authentication.
#     return (pipe);
# }
# 
# sub vcl_pass {
#     return (pass);
# }
# 
# sub vcl_hash {
#     set req.hash += req.url;
#     if (req.http.host) {
#         set req.hash += req.http.host;
#     } else {
#         set req.hash += server.ip;
#     }
#     return (hash);
# }
# 
# sub vcl_hit {
#     if (!obj.cacheable) {
#         return (pass);
#     }
#     return (deliver);
# }
# 
# sub vcl_miss {
#     return (fetch);
# }
# 
 sub vcl_fetch {
 
	   # vary on the custom header
	  if (beresp.http.Vary) {
		set beresp.http.Vary = beresp.http.Vary ", X-ABtesting";
	  } else {
		set beresp.http.Vary = "X-ABtesting";
	  }
     if (!beresp.cacheable) {
         return (pass);
     }
     if (beresp.http.Set-Cookie) {
         return (pass);
     }
     return (deliver);
 }
 
 sub vcl_deliver {
	if (resp.http.Vary) {
    set resp.http.Vary = regsub(resp.http.Vary, "X-ABtesting", "Cookie");
	}
	
     return (deliver);
 }
# 
# sub vcl_error {
#     set obj.http.Content-Type = "text/html; charset=utf-8";
#     synthetic {"
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
#  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
# <html>
#   <head>
#     <title>"} obj.status " " obj.response {"</title>
#   </head>
#   <body>
#     <h1>Error "} obj.status " " obj.response {"</h1>
#     <p>"} obj.response {"</p>
#     <h3>Guru Meditation:</h3>
#     <p>XID: "} req.xid {"</p>
#     <hr>
#     <p>Varnish cache server</p>
#   </body>
# </html>
# "};
#     return (deliver);
# }

4

2 回答 2

4

您正在寻找的是浏览器缓存和清漆缓存的不同 TTL。

如此处所述,最简单的解决方案是内部使用s-maxage指令Cache-Control,例如:

Cache-Control: s-maxage=31536000, max-age=300

其中31536000是 Varnish 缓存的秒数,300是您希望资源在浏览器中缓存的秒数。

为什么这样做是因为s-maxage覆盖了共享缓存(Varnish 是)的过期时间,而不适用于私有缓存(浏览器是)。

这是我认为最优雅的解决方案,但需要您调整后端应用程序。

这篇文章中概述了另一种解决方案,只需调整您的 VCL 即可完成:

    sub vcl_fetch {

            if (beresp.ttl > 0s) {
                    /* Remove Expires from backend, it's not long enough */
                    unset beresp.http.expires;

                    /* Set the clients TTL on this object */
                    set beresp.http.cache-control = "max-age=900";

                    /* Set how long Varnish will keep it */
                    set beresp.ttl = 1w;

                    /* marker for vcl_deliver to reset Age: */
                    set beresp.http.magicmarker = "1";
            }
    }

    sub vcl_deliver {
            if (resp.http.magicmarker) {
                    /* Remove the magic marker */
                    unset resp.http.magicmarker;

                    /* By definition we have a fresh object */
                    set resp.http.age = "0";
            }
    }
于 2018-02-26T17:43:21.113 回答
0

要控制浏览器端的缓存,您必须使用 Cache-Control 响应标头。见这里。您应该可以在 Drupal 代码中控制此标头。

于 2018-02-26T13:55:18.430 回答