Mailing List Archive

Retaining some cookies and receiving hits
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;
}
}}


=================================
Re: Retaining some cookies and receiving hits [ In reply to ]
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@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;
> }
> }}
>
>
> =================================
>
>
Re: Retaining some cookies and receiving hits [ In reply to ]
Hi,

As you said, the hashing is only based on the URL (and host/ip), that means
that Varnish can cache "/account.html" with cookie "user=alice" and deliver
it to the request "/account.html" with cookie "user=bob", is that an issue?

I highly recommend using vmod cookie to avoid the regex madness. I'd also
extract the cookies into their own headers and hash them inconditionally,
giving something like:

sub vcl_recv {
cookie.parse(req.http.cookie);
set req.http.cookie1 = cookie.get("COOKIE1");
set req.http.cookie2 = cookie.get("COOKIE2");
unset req.http.cookie;
}

sub vcl_hash {
hash_data(req.http.cookie1);
hash_data(req.http.cookie2);
}

--
Guillaume Quintard

On Tue, May 30, 2017 at 6:40 AM, sreeranj s <sreeranj4droid@gmail.com>
wrote:

> 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@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;
>> }
>> }}
>>
>>
>> =================================
>>
>>
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
Re: Retaining some cookies and receiving hits [ In reply to ]
Hi,

Why unset req.http.cookie; ?

Wouldn’t this require applications to read $_SERVER[‘HTTP_COOKIE1’] and $_SERVER[‘HTTP_COOKIE2’]. as opposed to $_COOKIE. (I can see this might break PHP sessions working out of the box).
Or I misunderstood?

Best Regards,
Danila

> On 30 May 2017, at 10:38, Guillaume Quintard <guillaume@varnish-software.com> wrote:
>
> Hi,
>
> As you said, the hashing is only based on the URL (and host/ip), that means that Varnish can cache "/account.html" with cookie "user=alice" and deliver it to the request "/account.html" with cookie "user=bob", is that an issue?
>
> I highly recommend using vmod cookie to avoid the regex madness. I'd also extract the cookies into their own headers and hash them inconditionally, giving something like:
>
> sub vcl_recv {
> cookie.parse(req.http.cookie);
> set req.http.cookie1 = cookie.get("COOKIE1");
> set req.http.cookie2 = cookie.get("COOKIE2");
> unset req.http.cookie;
> }
>
> sub vcl_hash {
> hash_data(req.http.cookie1);
> hash_data(req.http.cookie2);
> }
>
> --
> Guillaume Quintard
>
> On Tue, May 30, 2017 at 6:40 AM, sreeranj s <sreeranj4droid@gmail.com <mailto:sreeranj4droid@gmail.com>> wrote:
> 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@gmail.com <mailto:sreeranj4droid@gmail.com>> wrote:
> Hi,
>
> As per the link https://varnish-cache.org/docs/4.0/users-guide/increasing-your-hitrate.html <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;
> }
> }
> }
>
> =================================
>
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org <mailto:varnish-misc@varnish-cache.org>
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc <https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc>
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
Re: Retaining some cookies and receiving hits [ In reply to ]
Hello Danila,

Since hashing only the url, I assumed the cookie was of no interest to the
backend, so I'm nuking it to benefit from builtin.vcl, ie. I don't have to
return(hash).

--
Guillaume Quintard

On Tue, May 30, 2017 at 1:06 PM, Danila Vershinin <ciapnz@gmail.com> wrote:

> Hi,
>
> Why unset req.http.cookie; ?
>
> Wouldn’t this require applications to read $_SERVER[‘HTTP_COOKIE1’]
> and $_SERVER[‘HTTP_COOKIE2’]. as opposed to $_COOKIE. (I can see this might
> break PHP sessions working out of the box).
> Or I misunderstood?
>
> Best Regards,
> Danila
>
> On 30 May 2017, at 10:38, Guillaume Quintard <guillaume@varnish-software.
> com> wrote:
>
> Hi,
>
> As you said, the hashing is only based on the URL (and host/ip), that
> means that Varnish can cache "/account.html" with cookie "user=alice" and
> deliver it to the request "/account.html" with cookie "user=bob", is that
> an issue?
>
> I highly recommend using vmod cookie to avoid the regex madness. I'd also
> extract the cookies into their own headers and hash them inconditionally,
> giving something like:
>
> sub vcl_recv {
> cookie.parse(req.http.cookie);
> set req.http.cookie1 = cookie.get("COOKIE1");
> set req.http.cookie2 = cookie.get("COOKIE2");
> unset req.http.cookie;
> }
>
> sub vcl_hash {
> hash_data(req.http.cookie1);
> hash_data(req.http.cookie2);
> }
>
> --
> Guillaume Quintard
>
> On Tue, May 30, 2017 at 6:40 AM, sreeranj s <sreeranj4droid@gmail.com>
> wrote:
>
>> 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@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;
>>> }
>>> }}
>>>
>>>
>>> =================================
>>>
>>>
>>
>> _______________________________________________
>> varnish-misc mailing list
>> varnish-misc@varnish-cache.org
>> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>>
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
>
>
Re: Retaining some cookies and receiving hits [ In reply to ]
On Tue, May 30, 2017 at 1:27 PM, Guillaume Quintard
<guillaume@varnish-software.com> wrote:
> Hello Danila,
>
> Since hashing only the url, I assumed the cookie was of no interest to the
> backend, so I'm nuking it to benefit from builtin.vcl, ie. I don't have to
> return(hash).

On that note, a shameless plug:

https://info.varnish-software.com/blog/yet-another-post-on-caching-vs-cookies

Part 2 is written, still a draft that I need to revisit once my cookie madness
gauge leaves the red zone (the gauge is currently over 9000 and may fail to
block strong language).

On the same topic:

https://info.varnish-software.com/blog/sticky-session-with-cookies

Cheers

_______________________________________________
varnish-misc mailing list
varnish-misc@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc