Retaining some cookies and receiving hits

sreeranj s sreeranj4droid at gmail.com
Tue May 30 06:40:18 CEST 2017


Let me reiterate the question.

By default varnish will not cache if there is cookie present in request or
a set-cookie value is there in server response. In the following case we
have retained COOKIE1 and |COOKIE2, but can varnish  still caches the
responses(I have the unset cookie  from backend responses), by returning
hash in vcl recv. No changes in vcl_hash is made, so caching is based on
req_url.

Please advise on any issues on this approach.

***************************************
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,
";(COOKIE1|COOKIE2)=", "; \1=");
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

        if (req.http.Cookie == "") {
            unset req.http.Cookie;

            return(pass)
        }
      return(hash)

     }}

***************************************




On Mon, May 29, 2017 at 9:45 PM, sreeranj s <sreeranj4droid at gmail.com>
wrote:

> Hi,
>
> As per the link https://varnish-cache.org/docs/4.0/users-guide/
> increasing-your-hitrate.html, following code will help us to retain
> COOKIE1 and |COOKIE2, but strip other cookies. So COOKIE1 and |COOKIE2 is
> send to backend. I have the following questions.
>
> 1) By default varnish will not cache if there is cookie present in request
> or a set-cookie value is there in server response. In the following case we
> have retained COOKIE1 and |COOKIE2, but I still have varnish caches the
> responses(I have the unset cookie  from backend responses). Could you
> please let me know the reason.
>
> 2) If the approach is ok, please advise on any issues are related to this
> approach.
>
> 3) I am not adding any specific value in hash block, so requests are
> cached only based on req-url or IP. hope that is right.
>
>
> =================================
>
> 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, ";(COOKIE1|COOKIE2)=", "; \1=");
>         set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
>         set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
>
>         if (req.http.Cookie == "") {
>             unset req.http.Cookie;
>         }
>     }}
>
>
> =================================
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20170530/c81654fd/attachment-0001.html>


More information about the varnish-misc mailing list