Configure the Commerce application to use Varnish

To configure Commerce to use Varnish:

  1. Log in to the Admin as an administrator.

  2. Click Stores > Settings > Configuration > Advanced > System > Full Page Cache.

  3. From the Caching Application list, click Varnish Caching.

  4. Enter a value in the TTL for public content field.

  5. Expand Varnish Configuration and enter the following information:

    table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2 5-row-2
    Field Description
    Access list Enter the fully qualified hostname, IP address, or Classless Inter-domain Routing (CIDR) notation IP address range for which to invalidate content. See Varnish cache Purging.
    Backend host Enter the fully qualified hostname or IP address and listen port of the Varnish backend or origin server; that is, the server providing the content Varnish accelerates. Typically, this is your web server. See Varnish cache Backend servers.
    Backend port Origin server’s listen port.
    Grace period Determines how long Varnish serves stale content if the backend is not responsive. The default value is 300 seconds.

    Handles params size

    badge informative
    2.4.7-beta
    Available in 2.4.7-beta only
    Specifies the maximum number of layout handles to process on the {BASE-URL}/page_cache/block/esi HTTP endpoint for full-page caching. Restricting the size can improve security and performance. The default is 100.
  6. Click Save Config.

You can also activate Varnish from the command line–instead of logging in to the Admin—using the C command-line interface tool:

bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2

Export a Varnish configuration file

To export a Varnish configuration file from the Admin:

  1. Click one of the export buttons to create a varnish.vcl you can use with Varnish.

    For example, if you have Varnish 4, click Export VCL for Varnish 4

    The following figure shows an example:

    Configure Commerce to use Varnish in the Admin

  2. Back up your existing default.vcl. Then rename the varnish.vcl file you just exported to default.vcl. Then copy the file to the /etc/varnish/ directory.

    code language-bash
    cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak2
    
    code language-bash
    mv <download_directory>/varnish.vcl default.vcl
    
    code language-bash
    cp <download_directory>/default.vcl /etc/varnish/default.vcl
    
  3. Adobe recommend you open default.vcl and change the value of acl purge to the IP address of the Varnish host. (You can specify multiple hosts on separate lines or you can use CIDR notation as well.)

    For example,

    code language-conf
     acl purge {
        "localhost";
     }
    
  4. If you want to customize the Vagrant health checks or grace mode or saint mode configuration, see Advanced Varnish configuration.

  5. Restart Varnish and your web server:

    code language-bash
    service varnish restart
    
    code language-bash
    service httpd restart
    

Cache Static Files

Static files should not be cached by default, but if you want to cache them, you can edit the section Static files caching in the VCL to have the following content:

# Static files should not be cached by default
  return (pass);

# But if you use a few locales and do not use CDN you can enable caching static files by commenting previous line (#return (pass);) and uncommenting next 3 lines
  #unset req.http.Https;
  #unset req.http./* {{ ssl_offloaded_header }} */;
  #unset req.http.Cookie;

You must make these changes before you configure Commerce to use Varnish.

386822bd-e32c-40a8-81c2-ed90ad1e198c