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.
清漆为了学习,我只清理所有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"
Cookie图片(上面的文本重复):
好我们已经检查了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
我们要测试的内容:
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);
}
}
然后,我在Varnish之后组织了Nginx(仅用于记录请求)。 Nginx监听8090。这是日志配置:
log_format main '[$time_local] $http_cookie';
Nginx将请求传递到后端。后端位于端口8080。
总之在哪里听?
Varnish - 8000
Nginx - 8090
Backend - 8080
Cookies:
开始上光:
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,则>日志看起来像这样(仅供比较):
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"
好Cookie被截断。
问题:
请求不可避免地传递到后端。您是否愿意为我提供有关如何解决该问题的建议?
HTML
Varnish不查看Cookie
请求标头的内容,而是检查标头是否存在。