Skip to content

Varnish

Varnish Magento2 Configuration

  1. Install Varnish on the server
  2. Acquire a Magento VCL file using bin/magento varnish:vcl:generate
  3. Store the VCL file in /etc/varnish
  4. Enable use of Varnish cache in Admin > Stores > Configuration > System > Full Page Cache > Caching Application
    • update core_config_data set value = 2 where path = "system/full_page_cache/caching_application"
  5. Check the Varnish Configuration section's connection details
  6. Check HTTP headers for x-magento-cache-debug HIT

Troubleshooting

Large Header size issues can be potentially solved looking here

Increasing the workspace size can cause varnish to run out of memory. This can be confirmed by running the following

varnishlog | grep abandoned

If you see the following, then varnish has panicked. Log abandoned

Possible solutions

  • The workspace needs to be big enough for the largest category to fit comfortably within it
  • Customise Magento to make the headers smaller

Checking if a customer is logged in

When the full page cache is enabled, Magento 2 makes it impossible to check certain things in the customer session. This is done in \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml and happens immediately after layout generation is completed.

If you need to know if a customer is logged in or not but do not need any private identifiable information you can check this using the following the HTTP context class \Magento\Framework\App\Http\Context

/** @var \Magento\Framework\App\Http\Context */
$context = $this->context;

$isLoggedIn = $this->context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

This will return a boolean value.