Varnish cache enabled but still getting age: 0 in header

apache-2.4Apache2varnish

I have installed varnish on my server alongside apache. Its an SSL site. So I use apache for SSL termination with varnish. The problem is when I use the curl -I command, the headers show that age is always more than 0. Same case when I test my site with https://isvarnishworking.uk But, when I see the headers from my browser, it always shows the age as 0. I have tried everything. I even tried it with an http only site. Still in the web browser, the age is shown as 0

the guide that I used: https://bash-prompt.net/guides/apache-varnish/

These are the headers

accept-ranges: bytes
access-control-allow-origin: *
age: 0
content-encoding: gzip
content-length: 7128
content-type: text/html; charset=UTF-8
date: Thu, 15 Aug 2019 05:37:53 GMT
last-modified: Thu, 15 Aug 2019 04:25:12 GMT
server: Apache/2.4.29 (Ubuntu)
status: 200
vary: X-Forwarded-Proto,Accept-Encoding
via: 1.1 varnish (Varnish/5.2)
x-varnish: 65690

EDIT:
This is the headers that i get when i use curl -v 127.0.0.1:8080

Rebuilt URL to: 127.0.0.1:8080/
  Trying 127.0.0.1...
TCP_NODELAY set
Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
GET / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: curl/7.58.0
Accept: */*
HTTP/1.1 200 OK
Date: Thu, 15 Aug 2019 09:58:23 GMT
Server: Apache/2.4.29 (Ubuntu)
Link: ; rel="https://api.w.org/", https://arcadesite.io/>; rel=shortlink
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=UTF-8
X-Varnish: 491654 524537
Age: 62
Via: 1.1 varnish (Varnish/5.2)
Accept-Ranges: bytes
Content-Length: 28467
Connection: keep-alive

Best Answer

Why you don't get the same results in browsers and curl is mostly always one - cookies.

Varnish does some precautions for you and bypasses caching in presence of cookies (either when set by the backend, or when you're sending by the browser).

In your case it's most likely that your browser sends a Cookie: in request (can be easily verified in Chrome devtools, Network tab).

To make Varnish do its thing, the typical resolution is configuring it to strip all cookies when no essential cookies are present, e.g. in vcl_recv:

if (req.http.cookie !~ "your-app-cookie-name") {
    unset req.http.cookie;
}

This will take care of only passing through cookies when the your-app-cookie-name is one of the cookies being sent. For other cases (e.g. user not logged, the cookies are likely needed by Javascript tracking scripts, so not essential neither to Varnish nor backend to work properly).

If your app sends "essential" cookie for every page (quote often), you will need to either adjust your app (best route, e.g. send PHPSESSID cookie only in login page) and/or adjust Varnish config to ignore it on specific pages to increase cache hit-ratio.

Related Topic