How to Verify Your Website is Using Varnish Cache and Understand the Headers

NHAILA Achraf
3 min readMay 27, 2024

--

Are you curious if your website is leveraging the power of Varnish Cache for improved performance? In this article, we will guide you through the process of verifying that Varnish Cache is in action and explain how to interpret the response headers to ensure everything is working correctly.

Setting Up Varnish Cache with Docker

First, let’s quickly set up Varnish Cache using Docker. Here’s a simple docker-compose.yaml file to get you started:

version: "3"
services:
varnish:
image: varnish:stable
container_name: varnish
volumes:
- "./default.vcl:/etc/varnish/default.vcl"
ports:
- "80:80"
tmpfs:
- /var/lib/varnish:exec
environment:
- VARNISH_SIZE=2G
command: "-p default_keep=300"
depends_on:
- "httpd"
httpd:
image: httpd:latest
container_name: httpd
ports:
- "8080:80"

VCL Configuration

Create a default.vcl file with the following content:

vcl 4.1;

backend default {
.host = "httpd";
.port = "80";
}

Launching the Services

Run the following command to start your services:

docker-compose up -d

Verifying Varnish Cache Usage

To verify that Varnish Cache is in action, we will use curl to inspect the HTTP response headers.

Step 1: Make the Initial Request

Run this command to make an initial request to Varnish:

curl -I http://localhost

Expected Response Headers:

HTTP/1.1 200 OK
Date: Mon, 27 May 2024 09:18:39 GMT
Server: Apache/2.4.59 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Content-Length: 45
Content-Type: text/html
X-Varnish: 32
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

Understanding the Headers

  • X-Varnish: The first number is the current request ID.
  • Age: This header shows how long the object has been in the cache. An Age: 0 indicates a cache MISS because the object was fetched from the backend.
  • Via: This indicates that the request passed through Varnish.

Step 2: Make the Same Request Again

Run the command again:

curl -I http://localhost

Expected Response Headers for Cache HIT:

HTTP/1.1 200 OK
Date: Mon, 27 May 2024 09:19:00 GMT
Server: Apache/2.4.59 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Content-Length: 45
Content-Type: text/html
X-Varnish: 33 32
Age: 20
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

Understanding the Headers

  • X-Varnish: The first number is the current request ID, and the second number is the ID of the request that populated the cache.(number 1)
  • Age: This header shows how long the object has been in the cache. An Age: 0 indicates a cache MISS because the object was fetched from the backend. (number 2)
  • Via: This indicates that the request passed through Varnish.(number 3)

Additional Verification Steps

Inspect Full Headers and Body

You can use curl to see the full headers and body of the response:

curl -v http://localhost

Check Varnish Logs

To get detailed logs, access the Varnish container and use varnishlog:

docker-compose exec varnish bash
varnishlog

This command will provide detailed logs on how each request is handled, showing cache HITs and MISSes.

By following these steps, you can easily verify that your website is using Varnish Cache and understand how to interpret the response headers.

i hope this will help ,If you have any further questions or run into issues, don’t hesitate to reach out.

NHAILA Achraf

#varnish #cache #devsecops #devops #dockerswarm #swarm #cluster #datadog #azure #monitoring

--

--

NHAILA Achraf
NHAILA Achraf

No responses yet