Magento 2 Varnish Fixes Module
Magento 2 module which fixes issues with cache tags and 503 errors when using Varnish as caching application., (*1)
Install via composer:, (*2)
composer require trive/varnish
Modify your VCL file with the following content: ``` sub vcl_backend_response { ...(redacted), (*3)
# Trive - add these two lines below - start # collect all x-magento-tags headers std.collect(beresp.http.X-Magento-Tags); # Trive - add these two lines above - end" return (deliver);
} ```, (*4)
Enable the module:, (*5)
bin/magento module:enable Trive_Varnish bin/magento setup:upgrade
When using Varnish as caching application in Magento 2, each page is tagged by cache tags of the content displayed on it. These tags are then used for cache invalidation and purges, once content is updated., (*6)
For example, if you open a home page on default installation, it will be tagged with
store, cms_page and cms_page_1
cache tags., (*7)
Categories are tagged with cache tags of all products they have assigned. This means that, if you have a large catalog, with a single category containing about 2000 products, you're likely to have about 20kB worth of cache tags when first loading that category., (*8)
Cache tags are sent to Varnish as X-Magento-Tags
header on initial, non-cached request to Varnish, so it can tag the URL with displayed content, and be able to clear all pages where a object appears, when requested., (*9)
Once X-Magento-Tags
header gets longer than 8kB, default http_resp_hdr_len
Varnish limit is hit and Varnish returns a 503 error to customers (backend fetch failed)., (*10)
Even after implementing workaround from Magento Documentation, the issue is not solved, because another limit on Apache/nginx HTTP server might be hit, as they both have 8kB max header value by default., (*11)
Your store is affected if you are:
* using Magento 2 (any version)
* having a large number of products on store, with at least one anchor category containing at least 2000 products
* using Varnish as Magento 2 caching application, with default http_resp_hdr_len
limit, (*12)
Your store might also be affected if you are:
* using Apache/nginx with default LimitRequestFieldSize
, large_client_header_buffers
, or some other limit that might be hit by HTTP headers
* using nginx/HAProxy with default header length or request size limits
* using Apache mod_proxy_fcgi, (*13)
This module addresses the above issue by separating X-Magento-Tags
into 8kB header blocks.
Sending multiple X-Magento-Tags
headers don't hit most of the limits, and doesn't triggers 503, nor FetchError http read error: overflow
errors during Varnish backend fetch., (*14)
Note that default Magento 2 VCL file should be updated after implementing this module, to be able to collect multiple X-Magento-Tags
headers into one, during Varnish procesing., (*15)