Configuring Varnish for Caching based on Cookies
Pinakee BIswas
pinakee at waltzz.com
Tue May 24 08:18:18 CEST 2016
Hi Per Buer,
Thanks for your response and for the tips. Would go through them and try
to implement.
Regarding the blank pages, I have sent a separate mail.
I have installed Varnish from source. Since this is the first time with
Varnish, we plan to start with simple scenarios for caching and then
move on to more sophisticated ones for logged in users etc (also, as we
scale up further).
Would really appreciate if you could help me resolving the issue so that
I could move ahead with configuring and deploying Varnish. Following is
the content of the mail with the issue:
Hi,
I am trying to set Varnish to cache content when login cookie is not
present. Hence, I have modified the following subroutines:
* vcl_recv: If the login cookie is not present, return hash.
* vcl_back_response: If the login cookie is not present, cache the
content (beresp.uncacheable set to false
Whenever the vcl_recv is returning hash, varnish is generating blank
page with content-length set 0. If vcl_recv returns pass, the page is
served fine.
Following is the VCL:
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you
don't need,
# rewriting the request, etc.
set req.backend_hint = uwsgi;
if (req.http.Cookie !~ "jivaana_user=") {
return(hash);
}
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie
headers
# and other mistakes your backend does.
if (beresp.http.Set-Cookie !~ "jivaana_user=") {
if (beresp.ttl <= 0s) {
set beresp.ttl = 120s;
}
set beresp.uncacheable = false;
return (deliver);
}
Varnish log:
* << BeReq >> 3
- Begin bereq 2 fetch
- Timestamp Start: 1463994442.986360 0.000000 0.000000
- BereqMethod GET
- BereqURL /catalogue/
- BereqProtocol HTTP/1.0
- BereqHeader Host: django_test
- BereqHeader Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
- BereqHeader Upgrade-Insecure-Requests: 1
- BereqHeader User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X
10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102
Safari/537.36
- BereqHeader Accept-Language: en-US,en;q=0.8
- BereqHeader X-Forwarded-For: 127.0.0.1
- BereqHeader Accept-Encoding: gzip
- BereqProtocol HTTP/1.1
- BereqHeader X-Varnish: 3
- VCL_call BACKEND_FETCH
- VCL_return fetch
- BackendOpen 21 boot.uwsgi 127.0.0.1 9001 127.0.0.1 38213
- Timestamp Bereq: 1463994442.986883 0.000524 0.000524
- Timestamp Beresp: 1463994443.734972 0.748612 0.748088
- BerespProtocol HTTP/1.1
- BerespStatus 200
- BerespReason OK
- BerespHeader Expires: Mon, 23 May 2016 09:07:23 GMT
- BerespHeader Vary: Cookie
- BerespHeader Last-Modified: Mon, 23 May 2016 09:07:23 GMT
- BerespHeader Cache-Control: max-age=0
- BerespHeader X-Frame-Options: SAMEORIGIN
- BerespHeader Content-Type: text/html; charset=utf-8
- BerespHeader Set-Cookie:
csrftoken=5BUZRL4gdO7uTDtWXnbqQko3CelDmOAg; expires=Mon, 22-May-2017
09:07:23 GMT; Max-Age=31449600; Path=/
- BerespHeader Date: Mon, 23 May 2016 09:07:23 GMT
- TTL RFC 0 10 -1 1463994444 1463994444 1463994443 1463994443 0
- VCL_call BACKEND_RESPONSE
- TTL VCL 120 10 0 1463994444
- VCL_return deliver
- Storage file s0
- ObjProtocol HTTP/1.1
- ObjStatus 200
- ObjReason OK
- ObjHeader Expires: Mon, 23 May 2016 09:07:23 GMT
- ObjHeader Vary: Cookie
- ObjHeader Last-Modified: Mon, 23 May 2016 09:07:23 GMT
- ObjHeader Cache-Control: max-age=0
- ObjHeader X-Frame-Options: SAMEORIGIN
- ObjHeader Content-Type: text/html; charset=utf-8
- ObjHeader Set-Cookie:
csrftoken=5BUZRL4gdO7uTDtWXnbqQko3CelDmOAg; expires=Mon, 22-May-2017
09:07:23 GMT; Max-Age=31449600; Path=/
- ObjHeader Date: Mon, 23 May 2016 09:07:23 GMT
- Fetch_Body 0 none -
- BackendReuse 21 boot.uwsgi
- Timestamp BerespBody: 1463994443.735229 0.748869 0.000257
- Length 0
- BereqAcct 393 0 393 336 0 336
- End
* << Request >> 2
- Begin req 1 rxreq
- Timestamp Start: 1463994442.986091 0.000000 0.000000
- Timestamp Req: 1463994442.986091 0.000000 0.000000
- ReqStart 127.0.0.1 56147
- ReqMethod GET
- ReqURL /catalogue/
- ReqProtocol HTTP/1.0
- ReqHeader Host: django_test
- ReqHeader Connection: close
- ReqHeader Cache-Control: max-age=0
- ReqHeader Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
- ReqHeader Upgrade-Insecure-Requests: 1
- ReqHeader User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X
10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102
Safari/537.36
- ReqHeader Accept-Encoding: gzip, deflate, sdch
- ReqHeader Accept-Language: en-US,en;q=0.8
- ReqHeader Cookie: pagemap=0,0,0,0,0,1,0';
pagemap=0,0,0,0,0,0,0; jivaana_country=IN; mp_mixpanel__c=1;
mp_mixpanel__c3=2; mp_mixpanel__c4=2; mp_mixpanel__c5=0;
_ga=GA1.1.1762020660.1447765749;
mp_774636c1ed2371eaf99455f71871069c_mixpanel=%7B%22distinct_id%22%3A%20
- ReqHeader If-Modified-Since: Mon, 23 May 2016 09:03:11 GMT
- ReqHeader X-Forwarded-For: 127.0.0.1
- VCL_call RECV
- ReqUnset Cookie: pagemap=0,0,0,0,0,1,0';
pagemap=0,0,0,0,0,0,0; jivaana_country=IN; mp_mixpanel__c=1;
mp_mixpanel__c3=2; mp_mixpanel__c4=2; mp_mixpanel__c5=0;
_ga=GA1.1.1762020660.1447765749;
mp_774636c1ed2371eaf99455f71871069c_mixpanel=%7B%22distinct_id%22%3A%20
- VCL_return hash
- ReqUnset Accept-Encoding: gzip, deflate, sdch
- ReqHeader Accept-Encoding: gzip
- VCL_call HASH
- VCL_return lookup
- VCL_call MISS
- VCL_return fetch
- Link bereq 3 fetch
- Timestamp Fetch: 1463994443.735287 0.749196 0.749196
- RespProtocol HTTP/1.1
- RespStatus 200
- RespReason OK
- RespHeader Expires: Mon, 23 May 2016 09:07:23 GMT
- RespHeader Vary: Cookie
- RespHeader Last-Modified: Mon, 23 May 2016 09:07:23 GMT
- RespHeader Cache-Control: max-age=0
- RespHeader X-Frame-Options: SAMEORIGIN
- RespHeader Content-Type: text/html; charset=utf-8
- RespHeader Set-Cookie:
csrftoken=5BUZRL4gdO7uTDtWXnbqQko3CelDmOAg; expires=Mon, 22-May-2017
09:07:23 GMT; Max-Age=31449600; Path=/
- RespHeader Date: Mon, 23 May 2016 09:07:23 GMT
- RespHeader X-Varnish: 2
- RespHeader Age: 0
- RespHeader Via: 1.1 varnish-v4
- VCL_call DELIVER
- VCL_return deliver
- Timestamp Process: 1463994443.735341 0.749249 0.000054
- RespHeader Accept-Ranges: bytes
- RespHeader Content-Length: 0
- Debug "RES_MODE 2"
- RespHeader Connection: close
- Timestamp Resp: 1463994443.735410 0.749319 0.000070
- ReqAcct 929 0 929 476 0 476
- End
* << Session >> 1
- Begin sess 0 HTTP/1
- SessOpen 127.0.0.1 56147 0.0.0.0:9003 127.0.0.1 9003
1463994442.985890 18
- Link req 2 rxreq
- SessClose RESP_CLOSE 0.750
- End
Thanks,
Pinakee
On 23/05/16 11:21 pm, Per Buer wrote:
>
>
> On Fri, May 20, 2016 at 4:38 PM, Pinakee BIswas <pinakee at waltzz.com
> <mailto:pinakee at waltzz.com>> wrote:
>
> Hi,
>
> I am a beginner with Varnish and planning to deploy it for our
> ecommerce live platform. I am trying to learn Varnish and VCL and
> having trouble for our specific scenario:
>
> I would like to cache pages when a user is not logged in - and
> this is determined by a cookie (which is standard). The ecommerce
> platform is based on python Django - and there are bunch of other
> cookies which are not relevant for caching but they can't be
> unset/removed as they are needed by the origin/backend server.
>
> Hence, the solution would be to check if the login cookie is
> present or not. If login cookie not present, cache the page and
> fetch from cache else pass it to the backend.
>
>
> Two tips. One is the cookie VMOD, which helps when dealing with
> cookies. If you don't wanna use it you can also look at the example
> here:
> https://www.varnish-cache.org/docs/4.1/users-guide/increasing-your-hitrate.html#cookies
> - it will at least point you in the right direction.
>
> I tried implementing the above logic in vcl_recv and
> vcl_backend_response with my limited knowledge, but varnish
> started serving blank pages.
>
>
> That sounds like a backend bug.
>
> --
> *Per Buer*
> CTO | Varnish Software AS
> Cell: +47 95839117
> We Make Websites Fly!
> www.varnish-software.com <https://www.varnish-software.com/>
>
>
> <http://info.varnish-software.com/signature>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20160524/2f5beb42/attachment-0001.html>
More information about the varnish-misc
mailing list