0
votes

Due some odd reason varnish is not caching my response. Maybe it is easy but i can not find the reason why. As you can see below the response header has the Cache-Control header set with an expire date that is 1 year from now. Also the Pragma is set even though this is not the main way of doing it nowadays.

Request

GET /configurator/init/getPrices?website_id=2&type=ACQ&packageType=mobile&products=&sales_id= HTTP/1.1
Host: telesales.dev.dynacommerce.io
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,nl;q=0.8,de;q=0.7
Cookie: frontend=832pk4e9hqv0j2dmni1uuub5sl; PHPSESSID=vrqooasho3vgnhofkkmr3sb754

Response

Accept-Ranges: bytes
Age: 0
Cache-Control: max-age=31536000, must-revalidate
Content-Encoding: gzip
Content-Length: 2931
Content-Type: text/html; charset=utf-8
Date: Fri, 30 Mar 2018 11:30:43 GMT
Expires: Sat, 30 Mar 2019 11:30:43 +0000
Pragma: cache
Set-Cookie: PHPSESSID=rcv2tuirqtvujqt176gbocjbhg; expires=Fri, 30-Mar-2018 12:30:43 GMT; Max-Age=3600; path=/; domain=telesales.dev.dynacommerce.io; secure; HttpOnly
Set-Cookie: SIMPLESERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Vary: Accept-Encoding
X-Cache: MISS
X-Cache-Hits: 0
X-Frame-Options: SAMEORIGIN

The VCL file im using for this is this one: https://github.com/mattiasgeniar/varnish-5.0-configuration-templates/blob/master/default.vcl

Its a global boilerplate since 99% of the caching is done default and should be set on application level anyhow.

What i notice is that the issue is on the Cookies:

# Set 2min cache if unset for static files
  if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
    set beresp.ttl = 120s; # Important, you shouldn't rely on this, SET YOUR HEADERS in the backend
    set beresp.uncacheable = true;
    return (deliver);

However what i need is to force to cache this on a few endpoints. I now set a custom header from the application called X-Cache-Override: 1 to override it to cache (more like a force). But how to handle this in the VCL

1

1 Answers

-1
votes

The issue was the cookie. I solved it by adding a custom http header from the code to override the cookie setting. So in case this http header is set, varnish will cache it regardless the existence of the actual cookie itself

Update: What i have done in the backend response is doing this:

# If override is set, just ignore cookies regardless if they are set
  if (beresp.http.X-Cache-Override == "1") {
    unset beresp.http.set-cookie;
  }