代码之家  ›  专栏  ›  技术社区  ›  Cavva79

Varnish 4 vcl_deliver从backend_response中删除集合cookie

  •  2
  • Cavva79  · 技术社区  · 9 年前

    我有vcl_deliver的这种行为,它删除了每个http。从后端设置cookie。

    这是我的一块清漆:

    -   VCL_call       DELIVER
    -   RespUnset      Set-Cookie: JSESSIONID=20E1512F59F3BA8A7BAE6AC2C10B0F66; Path=/; HttpOnly
    -   RespUnset      Set-Cookie: OpenCmsOuFqn=/; Expires=Wed, 03-Feb-2016 13:18:41 GMT; Path=/
    -   RespUnset      Set-Cookie: OpenCmsUserName=Admin; Expires=Wed, 03-Feb-2016 13:18:41 GMT; Path=/
    -   RespHeader     Set-Cookie: LB=fep001; path=/;
    -   RespHeader     X-Cache: MISS
    -   VCL_return     deliver
    

    我可能看不到我发布的配置错误。 这是我的默认设置。vcl配置文件:

    vcl 4.0;
    
    import std;
    import directors;
    
    backend fep001 {
        .host = "fe1";
        .port = "82";
        .probe = {
            .url = "/ping";
            .interval = 10s;
            .timeout = 1s;
            .window = 1;
            .threshold = 1;
            .expected_response = 200;
        }
    }
    backend fep002 {
        .host = "fe2";
        .port = "82";
        .probe = {
            .url = "/ping";
            .interval = 10s;
            .timeout = 1s;
            .window = 1;
            .threshold = 1;
            .expected_response = 200;
        }
    }
    
    sub vcl_init {
        new cluster = directors.round_robin();
        new clusterhash = directors.hash();
        cluster.add_backend(fep001);
        clusterhash.add_backend(fep001, 1.0);
        cluster.add_backend(fep002);
        clusterhash.add_backend(fep002, 1.0);
    }
    
    sub vcl_recv {
        if (req.http.Cookie ~ "LB=fep[0-9]+") {
            set req.backend_hint = clusterhash.backend(req.http.Cookie.LB);
        } else {
            set req.backend_hint = cluster.backend();
        }
        if (! std.healthy(req.backend_hint)) {
            std.log("not healthy");
            set req.backend_hint = cluster.backend();
        }
    
        if (req.http.Cookie) {
            set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *LB=[^;]+;? *", "\1");
        }
    
        if (req.method != "GET" && req.method != "HEAD") {
            return(pass);
        }
        if (req.url ~ "^/export/.*$") {
            return(hash);
        }
        return(pass);
    }
    
    sub vcl_backend_response {
        set beresp.http.X-node = beresp.backend.name;
        set beresp.http.Vary = "Accept-Encoding";
        if (bereq.url ~ "^/export/.*$" && beresp.status < 400) {
            set beresp.ttl = 30m;
        } else {
            set beresp.ttl = 0s;
        }
        return(deliver);
    }
    
    sub vcl_deliver {
        if (obj.hits == 0 && req.http.Cookie !~ "LB=fep[0-9]+") {
            set resp.http.Set-Cookie = "LB=" + resp.http.X-node + "; path=/;";
        }
    
        if (obj.hits > 0) {
            set resp.http.X-Cache = "HIT:" + obj.hits;
        } else {
            set resp.http.X-Cache = "MISS";
        }
    }
    

    我如何保存这些http头?

    非常感谢。

    达维德

    1 回复  |  直到 9 年前
        1
  •  4
  •   Cavva79    4 年前

    我终于从文章中找到了一个解决方案 Proper sticky session load balancing in Varnish

    似乎Varnish 4不添加其他Set Cookie,而是覆盖它,并且不以以下方式添加此类Varnish 3:

    set resp.http.Set-Cookie = "LB=" + req.http.X-node + "; path=/;" + resp.http.Cookie;
    

    这意味着您必须使用一些VMOD。

    我添加了cookie和标头导入:

    vcl 4.0;
    
    import std;
    import directors;
    import cookie;
    import header;
    

    我改变了一点后端选择:

    cookie.parse(req.http.cookie);
    if (cookie.get("LB")) {
        set req.backend_hint = clusterhash.backend(cookie.get("LB"));
    } else {
        set req.backend_hint = cluster.backend();
    }
    if (! std.healthy(req.backend_hint)) {
        std.log("not healthy");
        set req.backend_hint = cluster.backend();
    }
    

    最后,这是添加这些VMOD的主要原因:

    if (obj.hits == 0 && req.http.Cookie !~ "LB=fep[0-9]+") {
        header.remove(resp.http.Set-Cookie,"^LB=.*$");
        header.append(resp.http.Set-Cookie,"LB=" + resp.http.X-node + "; Expires=" + cookie.format_rfc1123(now, 60m) + "; path=/;");
    }
    

    我希望这个答案会对某人有所帮助。