X-Magento-Vary cookie contains hash key which, collectively with page url, used as cache key in cache storage.
As in Magento 2, urls are not unique enough to be used as cache key for caching servers (such as Varnish), therefore, it uses Context Variables. Context variables are used to store different versions of same page based on customer group, selected store, currency, language and customer logged in or not. This context variable is then converted to hash and stored in x-magento-vary cookie.
We can also add our custom context variable to store different version of the page. In magento/module-page-cache module, we can see that there is a varnish configration file (.vcl) which reads X-Magento-Vary cookie to get context variables and process it further for caching. Magento provides such configuration as Varnish is a recommended caching server for Magento 2.
sub vcl_hash {
if (req.http.cookie ~ "X-Magento-Vary=") {
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
}
}
**This cookie is set as HTTPOnly which means this cookie will not be accessible using javascript.
Thank you. Happy Caching :)
Comments