Is er een probleem dat snel moet worden opgelost?Spoedfix nodig?Vraag spoedhulp aan →
Blog

Praktische Magento 2-gidsen over upgrades, beveiliging, snelheid en migratie, geschreven door het MageBooster-team.

Switching Magento 2.4.9 to Varnish with HTTPS: commands and config files

Varnish 8 behind nginx with HTTPS for Magento 2.4.9: every command and config file from MageBooster lesson 2, ready to copy or download, plus a built-in vs Varnish test.
Lesson 2MageBooster Lessons · Magento 2.4.9 · Ubuntu 24.04

Switching Magento to Varnish, with HTTPS

The commands and config files from the video, ready to copy or download: Varnish 8 behind nginx with HTTPS, the Magento admin settings, the VCL, and a side-by-side test against the built-in cache.

Bijgewerkt Oktober 2026Video about 6 minNiveau Serverbeheerders en developers

Downloads

Every file from the lesson. Change www.example.com, the certificate paths and /var/www/magento to your own before you use them.

How it fits together

Browser→nginx :443 HTTPS→Varnish 127.0.0.1:6081→nginx 127.0.0.1:8080→PHP-FPM · Magento

nginx keeps the certificate and hands every request to Varnish. Varnish answers from memory, or asks the second nginx server on port 8080, which runs Magento. Varnish listens on localhost only, so nobody reaches it from outside, and port 80 just redirects to HTTPS.

The start point is a running Magento 2.4.9 store served by nginx over HTTPS with the built-in full page cache, like the one from lesson 1.

1. Measure first

Time to first byte of a category page, three times, then a small load test with ab (from apache2-utils): a thousand requests, twenty at a time.

for i in 1 2 3; do curl -sk -o /dev/null -w 'TTFB %{time_starttransfer}s\n' https://www.example.com/women/tops-women.html; done

ab -q -n 1000 -c 20 https://www.example.com/women/tops-women.html | grep -E 'Requests per second|Time per request:.*mean\)'

2. Install Varnish 8

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish80/script.deb.sh | bash
apt install -y varnish && varnishd -V

3. nginx and HTTPS

Replace the site config. Three servers: port 80 redirects, port 443 keeps HTTPS and passes requests to Varnish, and 127.0.0.1:8080 runs Magento with the nginx.conf.sample that ships with it.

magento.confDownload

upstream fastcgi_backend { server unix:/run/php/magento.sock; }
server {
    listen 80;
    server_name www.example.com;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    http2 on;
    server_name www.example.com;
    ssl_certificate     /etc/nginx/ssl/www.crt;
    ssl_certificate_key /etc/nginx/ssl/www.key;
    location / {
        proxy_pass http://127.0.0.1:6081;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k;
        proxy_read_timeout 600s;
    }
}
server {
    listen 127.0.0.1:8080;
    server_name www.example.com;
    set $MAGE_ROOT /var/www/magento;
    include /var/www/magento/nginx.conf.sample;
}

Save it as /etc/nginx/conf.d/magento.conf. The certificate here is a self-signed test certificate; on a real domain see Let's Encrypt.

nginx -t && systemctl reload nginx

4. Varnish settings

A systemd override: Varnish on 127.0.0.1:6081, 1 GB of memory for the cache, and bigger header limits. Magento sends long X-Magento-Tags headers; without http_resp_hdr_len en http_resp_size big category pages end in a 503.

override.confDownload

[Service]
ExecStart=
ExecStart=/usr/sbin/varnishd -a 127.0.0.1:6081 -f /etc/varnish/default.vcl -P %t/%N/varnishd.pid \
  -s malloc,1g -p http_resp_hdr_len=65536 -p http_resp_size=98304

Save it as /etc/systemd/system/varnish.service.d/override.conf.

systemctl daemon-reload && systemctl restart varnish && ss -ltn | grep -E ':(443|6081|8080) '

5. Magento admin

Stores › Configuration › Advanced › System › Full Page Cache:

FieldValueWhat it does
Caching ApplicationVarnish Cache (Recommended)Magento sends cache headers for Varnish.
Access list127.0.0.1Who may send purge requests.
Backend host127.0.0.1Where Varnish finds Magento. Not localhost: it can resolve to IPv6 ::1 and give 503s.
Backend port8080The nginx server that runs Magento.
Grace period300Seconds Varnish may serve a stale page while it fetches a fresh one.

Save Config, then System › Cache Management › Flush Magento Cache. The same from the command line:

cd /var/www/magento
sudo -u magento bin/magento config:set system/full_page_cache/caching_application 2
sudo -u magento bin/magento config:set system/full_page_cache/varnish/access_list 127.0.0.1
sudo -u magento bin/magento config:set system/full_page_cache/varnish/backend_host 127.0.0.1
sudo -u magento bin/magento config:set system/full_page_cache/varnish/backend_port 8080
sudo -u magento bin/magento cache:flush

6. VCL and purges

varnish:vcl:generate does not read the admin values, so pass the same ones. Then tell Magento where to send purge requests.

cd /var/www/magento
sudo -u magento bin/magento varnish:vcl:generate --export-version=7 \
  --access-list=127.0.0.1 --backend-host=127.0.0.1 --backend-port=8080 > /etc/varnish/default.vcl

systemctl reload varnish
sudo -u magento bin/magento setup:config:set --http-cache-hosts=127.0.0.1:6081 -n

7. Test

The first request is a MISS, the second a HIT.

curl -skI https://www.example.com/women/tops-women.html | grep -iE '^HTTP|x-magento-cache-debug'
curl -skI https://www.example.com/women/tops-women.html | grep -iE '^HTTP|x-magento-cache-debug'

Built-in cache vs Varnish

Measured in the video, same store, same sample data, same server size, warm cache in both cases:

Built-inVarnish
Category page, time to first byte14 ms2 ms
Requests per second (ab, 20 at a time)5173,061 (6×)
A test VM, not a benchmark lab: your numbers will differ. The gap between the two is what counts, and it grows with traffic.

Why Varnish

  • Pages from memory in milliseconds. PHP and the database aren’t touched for cached pages.
  • Many more visitors on the same server. Campaigns and traffic spikes don’t take the store down.
  • Precise purges and grace mode. Magento purges only the pages that changed, by cache tags; visitors still get a page while Varnish refreshes it.
  • PHP stays free for what can’t be cached: the cart, the checkout and the admin. Varnish is what Adobe recommends for production.

Let’s Encrypt on a real domain

Let port 80 answer the certificate challenge and redirect everything else, then get the certificate with the webroot method. Renewals keep working with this config.

magento-letsencrypt.confDownload

upstream fastcgi_backend { server unix:/run/php/magento.sock; }
server {
    listen 80;
    server_name www.example.com;
    location /.well-known/acme-challenge/ { root /var/www/letsencrypt; }
    location / { return 301 https://$host$request_uri; }
}
server {
    listen 443 ssl;
    http2 on;
    server_name www.example.com;
    ssl_certificate     /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    location / {
        proxy_pass http://127.0.0.1:6081;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k;
        proxy_read_timeout 600s;
    }
}
server {
    listen 127.0.0.1:8080;
    server_name www.example.com;
    set $MAGE_ROOT /var/www/magento;
    include /var/www/magento/nginx.conf.sample;
}

Use this instead of magento.conf once the certificate exists; until then keep the test certificate lines.

apt install -y certbot
mkdir -p /var/www/letsencrypt
certbot certonly --webroot -w /var/www/letsencrypt -d www.example.com
nginx -t && systemctl reload nginx

Troubleshooting

SymptoomOorzaak en oplossing
503 Backend fetch failed on big category pagesHeaders too long: keep http_resp_hdr_len=65536 en http_resp_size=98304 in the override.
503 on every pageBackend host localhost resolved to ::1: generate the VCL with --backend-host=127.0.0.1.
Always MISSCaching Application is still Built-in, or a cookie/extension sends no-cache. Check with varnishlog -g request -q 'ReqURL ~ "tops-women"'.
Changes don’t show--http-cache-hosts missing or wrong port; purge requests never reach Varnish.
Redirect loop after the switchX-Forwarded-Proto https missing in the 443 server.
Zullen we het voor je instellen?We install and tune Varnish for Magento and Mage-OS stores, with Cloudflare or without.

▶ Watch every MageBooster lesson on YouTube: all lessons in order · subscribe to the channel

Deze pagina is automatisch vertaald. Zie je een fout? Laat het ons weten, dan passen we het meteen aan.

Gratis webshop-audit

Stuur ons de gegevens van je webshop. We controleren snelheid, beveiliging en extensies en sturen je binnen 3 werkdagen drie quick wins.

Laten we praten

Vraag een offerte aan

Vertel ons over je webshop, dan reageren we binnen één werkdag. Liever chatten? Stuur ons een WhatsApp

MageBooster Boek een UnoPim-demo Bekijk UnoPim live met producten zoals de jouwe. Een nieuwe PIM of een overstap van Akeneo: we sturen je een plan, een planning en een offerte.
✓ Officiële UnoPim-partner✓ AI-agent ingericht voor je team✓ Gekoppeld aan Magento
MageBooster Start je Hyvä-project Vanaf nul of vanuit je huidige webshop. We reageren met een projectplan, een tijdlijn en een offerte.
✓ Design en structuur door ons✓ Voor Hyvä geoptimaliseerde server✓ Bonusmodules inbegrepen

MageBooster

Wij nemen contact met je op

Laat je gegevens en het adres van je webshop achter. We bekijken eerst je webshop en nemen binnen één werkdag contact op, via telefoon, WhatsApp of e-mail, wat jij het liefst hebt.

Word partner