Search code examples
varnishvarnish-vcl

Combining headers in Varnish


I'm running multiple Varnish cache servers on top of each other. I want to "combine" the headers of each of them, that is, when I make a request to my website, I can see which cache server it had a hit on. Right now, both of the cache servers have this code:


sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
    if (obj.hits > 0) {
            set resp.http.X-Cache = "HIT";
    } else {
            set resp.http.X-Cache = "MISS";
    }
}

On my second cache server, I'd like the have something like this:

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
    if (obj.hits > 0) {
            set resp.http.X-Cache = "HIT, " + responsefromfirst;
    } else {
            set resp.http.X-Cache = "MISS, " + responsefromfirst;
    }
}

With responsefromfirst being the "X-Cache" header from the previous cache. How can I do this?


Solution

  • how about

    sub vcl_deliver {
        # Happens when we have all the pieces we need, and are about to send the
        # response to the client.
        #
        # You can do accounting or modifying the final object here.
        if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT, " + resp.http.X-Cache;
        } else {
                set resp.http.X-Cache = "MISS, " + resp.http.X-Cache;
        }
    }
    

    you really just want to prepend information to header that is already there.