How to Configure Varnish for Magento 2

created February 16, 2021, last updated February 16, 2021.

.
closeThis post was last updated 3 years 1 month 2 days ago, some of the information contained here may no longer be actual and any referenced software versions may have been updated!

Varnish and Magento 2 go together like Strawberries and Cream – you just cannot have one without the other.

Recently I got really confused about the correct way to configure Varnish for Magento 2, so for me and anyone else confused about the configuration here is the definitive guide to configuring Varnish for Magento 2.

The Definitive Guide to Configuring Varnish for Magento 2

To configure Varnish you need to know:

  • Varnish server name
  • Varnish listener TCP port – defaults to 6081
  • Magento content server name
  • Magento content server TCP port

If you are working with a single host the server name for Varnish and Magento will be localhost (127.0.0.1), if you are working in a Docker environment the server name for Varnish and Magento will be the container names of the Varnish and Magento web/content server services.

There are two areas in Magento where you must configure Varnish settings:

  1. Magento Core Config : app/etc/env.php
  2. Magento admin Stores -> Configuration -> System -> Full Page Cache

Core Config

The core config for Varnish in app/etc/env.php looks something like this:

    'http_cache_hosts' => [
        [
            'host' => 'varnish_server_hostname',
            'port' => '6081'
        ]
    ],
  • HOST=hostname / ip address of Varnish server
  • PORT=TCP listener port of Varnish server

You can configure these settings from the command line using:

php bin/magento setup:config:set --http-cache-hosts=varnish_server_hostname:6081

Admin Config

The admin config for Varnish in Stores -> Configuration -> System -> Full Page Cache looks something like this:

Magento 2 Varnish FPC Admin Configuration
Magento 2 Varnish FPC Admin Configuration
  • ACCESS LIST=hostname / ip address of Magento content server/s
  • BACKEND HOST=hostname / ip address of Magento content server/s
  • BACKEND PORT=TCP listener port of Magento content server
  • EXPORT CONFIGURATION=Click here to export a the Varnish VCL file

Note that the Varnish Configuration section settings here are only used to generate the Varnish VCL file.

If you are using Docker set the acl purge list in the Varnish VCL to all Docker private networks.

# purge set to docker nets
# 172.0.0.0/12 192.168.0.0/16
acl purge {
    "172.16.0.0"/12;
    "192.168.0.0"/16;
}

Troubleshooting

You can verify your Varnish configuration with n98-magerun2. Use the command:

n98-magerun2.phar config:show | grep full_page_cache

You should see something like the following where magento2_php-apache_1is the hostname of your Magento 2 content server and backend_port is the tcp port of the content (Magento) server

system/full_page_cache/caching_application - 2
system/full_page_cache/varnish/access_list - magento2_php-apache_1
system/full_page_cache/varnish/backend_host - magento2_php-apache_1
system/full_page_cache/varnish/backend_port - 8080
system/full_page_cache/varnish/grace_period - 300

Don’t get the ports mixed up :

  • By default Varnish is configured to listen for incoming external client http requests on TCP 6081.
  • The backend_port configured in admin is only used for the vcl config generation.
  • The env.php http_cache_hosts port is the port used to communicate with varnish.

To confirm your Varnish cache is working examine the headers returned by your Varnish server when browsing Magento frontend pages. You can also inspect the headers using curl

curl -I -H "host: magento2.gaiterjones.com" 127.0.0.1:80

X-Varnish: 762904605
Age: 0
X-Magento-Cache-Debug: HIT
Pragma: no-cache
Expires: -1
Cache-Control: no-store

Here we can see the X-Magento-Cache-Debug header showing a cache hit. Note – this header will be disabled in production mode.

Remember that Varnish has no support for TLS connections over HTTPS. To use an encrypted TLS connection to Magento 2 with Varnish FPC you need to use a frontend proxy such as NGINX.

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.