gpt4 book ai didi

varnish - 从缓存发出 Varnish 服务请求(cookies被清除)

转载 作者:行者123 更新时间:2023-12-03 17:47:07 34 4
gpt4 key购买 nike

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.

Varnish 出于学习目的,我只是清理所有 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 的图片(以上文字的副本):

enter image description here

行。我们已经检查了 Cookie 是否已设置。现在让我们检查一下 cookie 是否已正确清理。

我停在braakpoint并检查cookie。值为 {}。

enter image description here

好。所有 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';

    cookies :
  • 在 HTML 中,我组织了 Google Analytics 的跟踪。
  • 在后端,我设置了名为“sort_on”的cookie,其值为“title”。

  • +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    如果我们不切 cookies ,日志看起来是这样的(这只是为了比较):
    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

    开始 Varnish :
    michael@michael:~$ sudo varnishd -f /home/michael/PycharmProjects/varnish/varnish.vcl -a localhost:8000

    打开Chrome,多次加载页面:

    enter image description here

    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。没关系。 cookies 被切断。

    问题:请求不可避免地被传递到后端。我总是停在断点处。

    enter image description here

    您能否就如何处理这个问题给我一些建议?

    最佳答案

    Varnish 不查看 Cookie 的内容请求 header ,而是检查 header 是否存在。

    您需要做的是检查 Cookie header 是空的,如果是这样,只需删除整个内容。

    只需在您的 regsub 之后添加即可陈述:

    if (req.http.Cookie ~ "^\s*$") {
    unset req.http.Cookie;
    }

    让流程更灵活一点

    实际上,您可能不会删除所有 cookie,而只会删除对您的应用程序不重要的那些。

    您可以使用此语句删除所有 cookie,但您的应用程序需要的那些除外:
    sub vcl_recv {
    if (req.http.Cookie) {
    set req.http.Cookie = ";" + req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie,
    ";(SESS[a-z0-9]+|NO_CACHE)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie ~ "^\s*$") {
    unset req.http.Cookie;
    }
    else {
    return (pass);
    }
    }
    }

    此片段将删除所有 cookie,期望匹配 SESS[a-z0-9]+|NO_CACHE 的那些正则表达式。

    使用 vmod_cookie
    在 Varnish 中有一种更简洁的方法来处理 cookie 替换,它涉及使用 vmod_cookie Varnish 中的模块。你可以在这里找到它: https://github.com/varnishcache/varnish-cache/tree/master/lib/libvmod_cookie

    如果您升级到 Varnish 6.4 版, vmod_cookie将成为核心 Varnish 安装的一部分。

    这相当于使用 vmod_cookie :
    vcl 4.1;
    import cookie;

    sub vcl_recv {
    cookie.parse(req.http.cookie);
    cookie.keep_re("SESS[a-z0-9]+,NO_CACHE");
    set req.http.cookie = cookie.get_string();
    if (req.http.cookie ~ "^\s*$") {
    unset req.http.cookie;
    }
    }

    关于varnish - 从缓存发出 Varnish 服务请求(cookies被清除),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61203073/

    34 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com