Varnish
What is Varnish?
Varnish is an HTTP accelerator that caches responses in memory to serve repeat requests without hitting your app. In Laradock it sits behind Nginx: Nginx listens on 80/443 and forwards to Varnish, and Varnish forwards back to Nginx on port 81 (VARNISH_BACKEND_PORT). The shipped config was developed and tested for WordPress but likely works with other systems too; the approach is based on this Linode guide.
Start Varnish
Varnish must be built after Nginx, because its startup checks the domain's availability:
docker compose up -d nginx
docker compose up -d proxy
The service runs as two containers, proxy and proxy2, both defined in varnish/compose.yml.
Stop Varnish
docker compose stop proxy proxy2
Configuration
All settings live in varnish/defaults.env and can be overridden by adding the same line to your own .env:
| Variable | Default | What it does |
|---|---|---|
VARNISH_CONFIG | /etc/varnish/default.vcl | Path to the active VCL config inside the container. |
VARNISH_PORT | 6081 | Port Varnish listens on. |
VARNISHD_PARAMS | -p default_ttl=3600 -p default_grace=3600 | Extra flags passed to varnishd. |
VARNISH_PROXY1_CACHE_SIZE | 128m | Cache size for the first proxy (proxy). |
VARNISH_PROXY1_BACKEND_HOST | workspace | Domain/host the first proxy serves. |
VARNISH_PROXY1_SERVER | SERVER1 | Server label for the first proxy. |
VARNISH_PROXY2_CACHE_SIZE | 128m | Cache size for the second proxy (proxy2). |
VARNISH_PROXY2_BACKEND_HOST | workspace | Domain/host the second proxy serves. |
VARNISH_PROXY2_SERVER | SERVER2 | Server label for the second proxy. |
Configure a domain
- Set your domain in
VARNISH_PROXY1_BACKEND_HOST. - Update your Varnish config and add a matching Nginx config, using
nginx/sites/laravel_varnish.conf.exampleas a starting point. - Rename
default_wordpress.vcltodefault.vclto use the WordPress-tuned config instead of the olderdefault.vcl.
Serve multiple domains
- Add a second configuration section to
.env:VARNISH_PROXY1_CACHE_SIZE=128mVARNISH_PROXY1_BACKEND_HOST=replace_with_your_domain.nameVARNISH_PROXY1_SERVER=SERVER1 - Add a matching service to
varnish/compose.yml, modeled onproxy2:custom_proxy_name:container_name: custom_proxy_namebuild: ./varnishexpose:- ${VARNISH_PORT}environment:- VARNISH_CONFIG=${VARNISH_CONFIG}- CACHE_SIZE=${VARNISH_PROXY2_CACHE_SIZE}- VARNISHD_PARAMS=${VARNISHD_PARAMS}- VARNISH_PORT=${VARNISH_PORT}- BACKEND_HOST=${VARNISH_PROXY2_BACKEND_HOST}- BACKEND_PORT=${VARNISH_BACKEND_PORT}- VARNISH_SERVER=${VARNISH_PROXY2_SERVER}ports:- "${VARNISH_PORT}:${VARNISH_PORT}"links:- workspacenetworks:- frontend
Purge and reload
- Purge cache:
curl -X PURGE https://yourwebsite.com/ - Reload Varnish:
docker container exec proxy varnishreload - Reload Nginx:
docker exec Nginx nginx -tthendocker exec Nginx nginx -s reload
Allowed Varnish CLI commands inside the container: varnishadm, varnishd, varnishhist, varnishlog, varnishncsa, varnishreload, varnishstat, varnishtest, varnishtop.
Common issues
- Varnish container fails to build. It's expected to be built after Nginx is already up, since it checks the domain's availability at build/start time; run
docker compose up -d nginxfirst. - Stale content keeps being served. Purge the cache (
curl -X PURGE ...) or reload Varnish (varnishreload) after deploying changes. - Wrong backend served. Confirm
VARNISH_PROXY1_BACKEND_HOST/VARNISH_PROXY2_BACKEND_HOSTmatch the domain in the corresponding Nginx site config, and that you're usingdefault_wordpress.vcl(renamed todefault.vcl) if following the WordPress setup.
Varnish sits in front of Nginx; for balancing across multiple Varnish instances see HAProxy. New to Laradock? Start with Getting Started.