【发布时间】:2020-07-26 21:48:00
【问题描述】:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-54516992-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-54516992-1');
</script>
</head>
<body>
test
</body>
</html>
后端。这是 Python + Django。请不要害怕。我只是在这里设置了一个 cookie (render_to_response),然后我需要一个在断点处停止的地方(在代码下方,它通过注释显示它所在的位置)。
class HomeView(TemplateView):
template_name = "home/home.html"
def render_to_response(self, context, **response_kwargs):
response = super(TemplateView, self).render_to_response(context, **response_kwargs)
response.set_cookie('sort_on', 'title')
return response
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context # Breakpoint.
varnishd出于学习目的,我只清理所有 cookie。
$ varnishd -V
varnishd (varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2019 Varnish Software AS
VCL
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
call remove_proprietary_cookies;
if (req.http.Cookie) {
return (pass);
}
}
sub remove_proprietary_cookies{
set req.http.Cookie = regsuball(req.http.Cookie, ".*", "");
}
然后我重新加载页面几次以确保 cookie 确实存在。
在 Chrome 中:
document.cookie
"sort_on=title; _ga=GA1.1.988164671.1586849704; _gid=GA1.1.995393496.1586849704; _gat_gtag_UA_54516992_1=1"
cookies 的图片(以上文字的副本):
好的。我们已经检查了 Cookie 是否已设置。现在让我们检查一下 cookie 是否已正确清理。
我在braakpoint 停下来检查cookie。值为 {}。
嗯。所有 cookie 似乎都已清除。
问题: 重新加载时,我经常遇到断点。这意味着 Varnish 将请求传递到后端。换句话说 if (req.http.Cookie) 没有像我预期的那样工作。我希望在上一步中我已经删除了 cookie。然后我检查 cookie 是否存在。而且不应该有。
你能帮帮我吗:
了解这里发生了什么。
组织所有内容,这样如果我错误地删除了 cookie,我一定要删除它们。
让 Varnish 从缓存中处理此请求,而不将其传递到后端。
==============4月16日添加================= ++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++
我已将 Varnish 升级到 6.4:
michael@michael:~$ varnishd -V
varnishd (varnish-6.4.0 revision 13f137934ec1cf14af66baf7896311115ee35598)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2020 Varnish Software AS
我们要测试的内容:
- 在后端设置 Google Analytics cookie 和 cookie。
- 让 Varnish 删除所有 cookie 并缓存响应。
- 检查请求是否只传递到后端一次。
然后我在 Varnish 后面组织了 nginx(仅用于记录请求)。 Nginx 在 8090 监听。这是日志配置:
log_format main '[$time_local] $http_cookie';
Cookie:
- 我在 HTML 中组织 Google Analytics 的跟踪。
- 在后端,我设置了名为“sort_on”的 cookie,其值为“title”。
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
如果我们不切 cookie,日志看起来是这样的(这只是为了比较):
127.0.0.1 - - [16/Apr/2020:08:11:05 +0300] "GET / HTTP/1.1" sort_on=title; _ga=GA1.1.236170900.1587013419; _gid=GA1.1.2033785209.1587013419 200 334 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36" "127.0.0.1"
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++
varnish.ctl
vcl 4.1;
import cookie;
backend default {
.host = "127.0.0.1";
.port = "8090";
}
sub vcl_recv {
unset req.http.Cookie;
if (req.http.Cookie) {
return (pass);
}
}
Nginx 将请求传递到后端。后端位于 8080 端口。
简而言之,什么在哪里听:
Varnish - 8000
Nginx - 8090
Backend - 8080
开始清漆:
michael@michael:~$ sudo varnishd -f /home/michael/PycharmProjects/varnish/varnish.vcl -a localhost:8000
打开Chrome,多次加载页面:
Nginx的访问日志:
127.0.0.1 - - [16/Apr/2020:08:12:49 +0300] "GET / HTTP/1.1" - 200 334 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36" "127.0.0.1"
127.0.0.1 - - [16/Apr/2020:08:13:21 +0300] "GET / HTTP/1.1" - 200 334 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36" "127.0.0.1"
我们可以看到没有cookie。没关系。 Cookie 被切断。
问题:请求不可避免地被传递到后端。我总是停在断点处。
您能否就如何处理这个问题给我一些建议?
【问题讨论】:
标签: varnish varnish-vcl varnish-4