browser - 在浏览器上禁用缓存而不在 Varnish 上禁用它

标签 browser drupal varnish drupal-cache

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

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

如何在浏览器上禁用缓存以使其影响 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);
# }

最佳答案

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

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

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

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

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

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

this post 中概述了另一种解决方案。并且可以通过仅调整您的 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";
            }
    }

关于browser - 在浏览器上禁用缓存而不在 Varnish 上禁用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48988162/

相关文章:

apache - Varnish 的优雅关闭

Varnish 4 不支持缓存控制 : must-revalidate

javascript - IE 性能问题 - 慢

javascript - 如何在网站上制作一个按预定时间按钮的脚本(如 pg down 或 space)?

browser - 如何下载显示在网页上的文本文件

javascript - 如何检查身份不明的弹出窗口上的元素?

php - Drupal SQL表如何在每一行上插入编辑按钮

Drupal 8 包含部分模板

varnish - 调试 VCL 文件的最佳方法是什么?

android - 如何通过浏览器(android 和/或 iOS)从 Web 应用程序访问通话记录和短信?