How To Configure Varnish Cache In Magento 2

Varnish Cache is a powerful HTTP accelerator that significantly boosts Magento 2 performance by caching full-page responses. In this guide, you’ll learn How To Configure Varnish Cache In Magento 2 and connect it to your web server setup.

Steps To Configure Varnish Cache In Magento 2

Step 1: Enable Varnish in Magento 2

1. In the Magento Admin Panel, go to Stores > Configuration > Advanced > System

2. Under the Full Page Cache section:

  • Set Caching Application to Varnish Cache
  • Set TTL for public content (e.g., 86400 seconds)

Click Save Config

Step 2: Export Magento’s Varnish Configuration

1. Still in the Full Page Cache section, locate the Varnish Configuration subsection.

Configure the following:

  • Access List (e.g., localhost)
  • Backend Host: 127.0.0.1
  • Backend Port: 8080 (assuming your web server listens on this port)

Click Export VCL for Varnish 6

Magento will download a varnish.vcl file customized for your current store settings.

Step 3: Replace the Default Varnish Configuration

Upload the exported .vcl file to your server and replace the default configuration:

sudo mv /path/to/varnish.vcl /etc/varnish/default.vcl

Restart Varnish to apply the changes:

sudo systemctl restart varnish

Step 4: Verify Varnish Is Working with Magento

Use curl to inspect the response headers:

curl -I http://yourdomain.com

Look for a header like:

Via: 1.1 varnish

You can also enable developer hints or log headers to confirm cache hits/misses.

Bonus Tip: Handling HTTPS

Varnish does not support HTTPS directly. For SSL termination:

  • Use Nginx or Apache on port 443
  • Set them to forward HTTP requests to Varnish on port 80

This setup ensures secure access for customers while still benefiting from Varnish caching.

Integrating Magento 2 with Varnish Cache is a straightforward process once Varnish is installed. By exporting Magento’s VCL and linking it properly, you’ll significantly boost performance, reduce load times, and create a smoother shopping experience for your users.

Follow us for the more helpful posts!

We hope this is a useful post for you.

You can read more useful posts like How To Install Varnish Cache on Ubuntu.

5 1 vote
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x