2

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
6
  • What response headers is the internal HTTP server giving? If those tell varnish not to cache, then varnish won't (by default, can be overriden).
    – wurtel
    Aug 15, 2019 at 8:50
  • um how can I find out that? Aug 15, 2019 at 9:28
  • The same way you got the headers from varnish? From the server itself use the URL that varnish uses to access the internal HTTP server, which is probably 127.0.0.1:8080 if you followed that guide.
    – wurtel
    Aug 15, 2019 at 9:32
  • Added the Header output of curl -v 127.0.0.1:8080 in the post Aug 15, 2019 at 10:04
  • I followed the same guide and i set up a virtual host for a subdomain sub.example.com The weird thing is that the wordpress site hosted on the subdomain is being cached by varnish (age is more than 0) and the main website example.com is not. Even though both are on same server and both of them were configured using the guide mentioned above Aug 15, 2019 at 10:22

1 Answer 1

2

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.

2
  • I don't use any tracking services. Also, it seems to have got fixed after I changed my wordpress theme. Still, users don't need to be logged in to my website. Why for some particular themes permit to cache wordpress pages, I'm confused about this thing Aug 18, 2019 at 7:06
  • A particular theme may use cookies for remembering user preferences. Some even do this to allow users to change the preferred color scheme. As for tracking scripts, even use of Google Analytics (which sets a cookie of its own), may cause cacheability issues if the VCL does not follow on "cookie whitelisting" approach given in my answer. Aug 18, 2019 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .