Cavva79
Cavva79

Reputation: 399

Varnish 4 vcl_deliver removes set-cookie from backend_response

I've this behaviour of vcl_deliver, it removes every http.set-cookie from backend.

This is my piece of varnishlog:

-   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

I may not see the configuration mistake I post it too. This is my default.vcl config file:

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";
    }
}

How could I keep those http headers?

Thank you

Davide

Upvotes: 2

Views: 2997

Answers (1)

Cavva79
Cavva79

Reputation: 399

I finally found one solution deduced from the article Proper sticky session load balancing in Varnish

It seems that Varnish 4 does not add other Set-Cookie but override it and does not add such Varnish 3 in such ways:

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

It means that you have to use some VMODs.

I added cookie and header imports:

vcl 4.0;

import std;
import directors;
import cookie;
import header;

I changed a bit the backend selection:

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();
}

and in the end this is the main reason to add those VMODs:

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=/;");
}

I hope this answer will help someone.

Upvotes: 4

Related Questions