Updated: 28 September 2026 · Applies to: Ubuntu 24.04 (Varnish 7.1, Nginx)
Ubuntu 16.04 is end of life; this guide uses Ubuntu 24.04 (Varnish 7.1). Varnish is an HTTP cache: it keeps copies of pages in memory and serves them without asking the web server again. Varnish does not handle HTTPS itself, so the usual setup is:
visitor → Nginx :443 (HTTPS) → Varnish :6081 → Nginx :8080 (website)
1. Install
apt install nginx varnish
2. The website on port 8080
In your site's Nginx server block, listen on 127.0.0.1:8080 instead of port 80 (keep the root, index and PHP settings). Reload Nginx.
3. Point Varnish to it
In /etc/varnish/default.vcl:
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Restart Varnish: systemctl restart varnish. It listens on port 6081. Test with curl -I http://127.0.0.1:6081/: repeated requests show an Age header greater than 0 when a page comes from the cache.
4. HTTPS in front
Create a public Nginx server block on port 443 (with a Certbot certificate) that proxies to Varnish:
location / {
proxy_pass http://127.0.0.1:6081;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
Redirect port 80 to HTTPS in a small server block.
Note: Varnish does not cache requests with cookies by default, which covers logged-in users and shopping carts. For WordPress or shops, use a VCL written for your application, and exclude admin and checkout pages.