Configuring Varnish Cache for Magento 2 to Reduce TTFB
Learn how to implement Varnish Cache for Magento 2 to drastically reduce TTFB and server load by offloading HTML delivery to a high-performance reverse proxy.
15 Jun 2026, 03:02 UTC

The Performance Bottleneck: Application Overhead
Magento 2 is a resource-intensive application. Even with internal caching, the server must execute PHP and query the database to generate HTML for every request. This results in a high Time to First Byte (TTFB), which slows down the user experience and increases server CPU load during traffic spikes.
The solution is to implement Varnish as a reverse proxy. Varnish sits in front of your web server (Nginx or Apache) and stores the entire HTML output of your pages in memory. When a repeat request arrives, Varnish serves the page immediately without ever touching the Magento application stack.
Prerequisites
- Magento 2.x installation with a functional web server (Nginx or Apache).
- Varnish Cache installed on the server (typically version 6.0+).
- Root or sudo access to the server for configuration changes.
- A network configuration where Varnish can listen on port 80 and forward traffic to the web server on a different port (e.g., 8080).
Implementation Procedure
1. Configure the Web Server Port
Since Varnish must handle incoming HTTP requests on port 80, you must move your primary web server to a different port. If using Nginx, modify your site configuration file:
# Run on the web server as root/sudo
# Edit /etc/nginx/sites-available/magento
listen 8080;
Restart the web server to apply the change: systemctl restart nginx.
2. Apply the Magento VCL
Varnish uses Varnish Configuration Language (VCL) to determine what to cache and when to purge. Magento provides a default VCL file that handles complex logic like X-Magento-Tags for granular cache invalidation.
Copy the Magento VCL to your Varnish configuration directory:
# Run on the server as root/sudo
cp vendor/varnish/magento2-vcl/varnish.vcl /etc/varnish/default.vcl
Ensure the backend definition in default.vcl points to your web server's new port (8080) and IP address.
3. Enable Varnish in Magento Admin
Once Varnish is running and the VCL is loaded, you must tell Magento to send the correct cache headers.
- Log in to the Magento Admin Panel.
- Navigate to Stores > Settings > Configuration.
- Go to Advanced > System.
- Expand the Full Page Cache section.
- Set Caching to
Varnish Cache. - Save Config and flush the Magento cache:
bin/magento cache:flush.
Verification and Diagnostics
To verify that Varnish is actually serving the content rather than the web server, inspect the HTTP response headers using curl or browser developer tools.
Checking Cache Status
Run the following command from a terminal:
curl -I http://your-magento-domain.com/
Look for the X-Magento-Cache-Debug header. A successful configuration will show:
X-Magento-Cache-Debug: HIT
If you see MISS, refresh the page and check again. A HIT indicates the page was served from Varnish memory.
Monitoring Real-time Traffic
To diagnose why certain pages aren't caching or to monitor the hit ratio, use the varnishlog utility on the server:
# Run as root/sudo
varnishlog -g request
Comparison: Built-in Cache vs. Varnish
| Feature | Built-in Cache | Varnish Cache |
|---|---|---|
| Storage | Filesystem/Redis | RAM |
| Processing | Requires PHP execution | Bypasses PHP entirely |
| TTFB | Moderate | Ultra-Low |
| Complexity | Low (Out-of-box) | Medium (Requires VCL) |
Limitations and Risks
- Cache Poisoning: If the VCL is modified incorrectly, Varnish might cache a page intended for a specific logged-in user (containing private data) and serve it to all subsequent visitors. Always test VCL changes in a staging environment.
- Stale Content: If the connection between Magento and Varnish is broken, updates to products in the admin panel will not trigger a purge, leaving old prices or descriptions on the storefront.
- SSL Termination: Varnish does not handle HTTPS/SSL. You must use a load balancer or a web server (like Nginx) as an SSL terminator to decrypt traffic before passing it to Varnish.
Rollback Procedure
If Varnish causes instability or incorrect content delivery, revert to the built-in cache:
- Change Full Page Cache back to
Built-in Cachein the Magento Admin. - Update your web server configuration to listen on port 80 again:
listen 80;. - Restart the web server:
systemctl restart nginx. - Stop the Varnish service:
systemctl stop varnish.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.