Ova

How to enable varnish cache in Magento 2?

Published in Magento Caching 5 mins read

Enabling Varnish cache in Magento 2 is a crucial step for optimizing your store's performance and accelerating page load times. Varnish acts as a reverse proxy, serving cached pages directly to users, which significantly reduces the load on your Magento application and database.

How to Enable Varnish Cache in Magento 2?

To enable Varnish cache in Magento 2, you need to configure both your Magento admin panel and your Varnish server. This process involves selecting Varnish as the caching application, generating its configuration file, and deploying it on your Varnish server.

1. Prerequisites for Varnish Integration

Before configuring Magento, ensure Varnish is properly installed and running on your server. Magento 2 officially supports specific Varnish versions, typically Varnish 6.0 LTS or Varnish 7.0. Consult the Magento DevDocs for the latest compatibility information.

Your server setup should also route traffic through Varnish. This often involves configuring your web server (Apache or Nginx) to listen on a different port (e.g., 8080) and Varnish to listen on the standard HTTP port (80) or HTTPS port (443 if using a proxy like Nginx for SSL termination before Varnish).

2. Step-by-Step Guide to Configure Varnish in Magento Admin

The first part of enabling Varnish involves configuring your Magento installation:

Configure Magento for Varnish

  1. Access Admin Panel: Go to your Magento Admin panel.
  2. Navigate to Configuration: Select Stores > Configuration.
  3. Access System Settings: In the left-hand panel, under the Advanced section, click System.
  4. Expand Full Page Cache: Expand the Full Page Cache section.
  5. Select Caching Application: From the Caching Application dropdown, choose Varnish Cache.
  6. Adjust TTL (Optional): You can optionally modify the TTL for public content value. This sets the Time-To-Live for cached public pages, overriding the standard cache expiration. A higher value means content stays cached longer, reducing server load but potentially showing less fresh content.
  7. Enter Varnish Configuration Details:
    • Access list (comma separated): Enter the IP addresses that are allowed to send purge requests to Varnish (e.g., localhost, 127.0.0.1).
    • Backend host: Specify the IP address or hostname of your web server where Magento is running (e.g., 127.0.0.1 or the server's local IP).
    • Backend port: Enter the port your web server listens on (e.g., 8080 if Varnish is on port 80).
    • Grace period: Define the time Varnish can serve stale objects while fetching new ones from the backend.
  8. Save Configuration: Click Save Config to apply your changes.

Generate and Deploy Varnish Configuration File (VCL)

After configuring Magento, you need to generate and deploy the Varnish Configuration Language (VCL) file:

  1. Get VCL for Varnish: On the same Full Page Cache section in your Magento Admin, click the button corresponding to your Varnish version, for example, Get VCL for Varnish 6.0. This will download the default.vcl file.
  2. Transfer VCL to Varnish Server: Copy the downloaded default.vcl file to your Varnish server's configuration directory. Common locations include /etc/varnish/ or /usr/local/etc/varnish/. You may need to replace an existing default.vcl or ensure your Varnish service is configured to load this specific file.
  3. Configure Varnish Daemon (if necessary): Edit the Varnish system configuration file (e.g., /etc/default/varnish or /etc/sysconfig/varnish on Linux systems). Ensure the DAEMON_OPTS are set to:
    • Listen on the correct HTTP port (e.g., -a :80).
    • Use your generated default.vcl file (e.g., -f /etc/varnish/default.vcl).
    • Set the correct backend (e.g., -b 127.0.0.1:8080).
  4. Restart Varnish: Restart the Varnish service for the changes to take effect. Use commands like sudo systemctl restart varnish or sudo service varnish restart depending on your operating system.

3. Verifying Varnish Cache Functionality

After configuration and restart, it's crucial to verify that Varnish is actively caching your Magento store:

  • Browser Developer Tools:
    1. Open your browser's developer tools (usually by pressing F12).
    2. Navigate to the Network tab.
    3. Load a Magento page in your browser.
    4. Inspect the HTTP headers for the main document request (the first request for the HTML page).
    5. Look for specific headers indicating Varnish is active:
      • X-Magento-Cache: VARNISH confirms Magento is configured to use Varnish.
      • X-Varnish: <ID> indicates the request went through Varnish.
      • X-Varnish: <ID_hit> <ID_miss> or X-Varnish: <ID> (followed by Age: <seconds>) on subsequent requests for the same page confirms a cache hit. A hit means Varnish served the content from its cache. An Age value greater than 0 also confirms a hit.
      • X-Varnish: <ID_miss> or X-Varnish: <ID> (without Age header) on the first request for a page indicates a cache miss, which is normal as Varnish fetches it from the backend for the first time.

4. Managing Your Varnish Cache

Effective cache management is essential for a smooth-running store:

  • Clearing Magento Cache: From the Magento Admin, go to System > Tools > Cache Management. Flushing the "Magento Cache" or "Full Page Cache" types will automatically send a purge request to Varnish, clearing its relevant cached items.
  • Manually Purging Varnish (SSH): For advanced scenarios, you can manually purge Varnish via SSH using the varnishadm command. For example, varnishadm ban req.url "~" would ban all URLs (use with extreme caution as it clears everything). It's generally better to rely on Magento's cache flush.

5. Benefits of Using Varnish with Magento 2

Implementing Varnish cache brings significant advantages to your e-commerce store:

Feature Benefit
Accelerated Page Loads Drastically reduces Time To First Byte (TTFB), leading to faster pages.
Reduced Server Load Varnish serves static HTML pages directly, offloading Magento and PHP.
Improved Scalability Enables your store to handle a higher volume of concurrent users.
Enhanced User Experience Faster loading times improve customer satisfaction and conversion rates.
SEO Advantages Page speed is a ranking factor, positively impacting search visibility.

By following these steps, you can successfully integrate Varnish with your Magento 2 store, unlocking substantial performance improvements and a better experience for your customers.