Varnish remove specific cookies from backend response

Martin Taleski picture Martin Taleski · Dec 30, 2012 · Viewed 8k times · Source

I need to remove specific cookies from the backend response in varnish.

My backend server sets a bunch of cookies that I don't need and unfortunately I can not control, so I want to delete them.

However I need some of the cookies, so I want to be able to remove cookies by their name.

For example I want to rename a cookie named bad_cookie, but at the same time keep a cookie named good_cookie.

I have found a lot of resources about removing specific request cookies, but none about removing backend response cookies.

Is this possible in Varnish?

Answer

matiasrege picture matiasrege · Jan 3, 2013

If you want to rename I think it would be something like:

sub vcl_fetch {
    #renamed after receiving the backend
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "bad_cookie", "good_cookie"); 
    set beresp.http.cookie = regsuball(beresp.http.cookie, "bad_cookie", "good_cookie"); }
}

sub vcl_deliver {
    #renamed before sending the client
    set resp.http.set-cookie = regsuball(beresp.http.set-cookie, "bad_cookie", "good_cookie"); 
    set resp.http.cookie = regsuball(beresp.http.cookie, "bad_cookie", "good_cookie"); }
}

If you want to delete all cookies:

sub vcl_fetch {
    #deleted after receiving the backend
    remove beresp.http.set-cookie;
    remove beresp.http.cookie;
}

sub vcl_deliver {
    #deleted before sending the client
    remove resp.http.set-cookie;
    remove resp.http.cookie;
}

beresp.http.set-cookie reads only the first Set-Cookie header, If you want to delete some and keep others can use: github.com/varnish/libvmod-header**