1

I am using varnish with default magento 2.3.3 VCL and the default nginx configuration that comes with magento install. At present I see the expires is set to -1. How can I set expires to one week?

enter image description here

5

At present I see the expires is set to -1

The Expires header you see is client-side. Browsers should not cache things like product pages, etc. which is why the default Magento VCL sets it to -1 in vcl_deliver procedure.

The vcl_deliver is what you use in Varnish for controlling the output to the clients (e.g. browsers).

How long Varnish will cache things from Magento depends on different things: the Expires and Cache-Control headers it sees from its backend (NGINX + PHP-FPM).

So while your client (curl) sees -1, Varnish uses/sees different Expires. You can confirm the same by issuing a curl request directly against NGINX backend port (e.g. 8080, if that's where it is listening) to literally see the same headers which Varnish will see.

curl -IL http://localhost:8080/foo.html -H "Host: www.example.com" -H "X-Forwarded-Proto: https"

This would yield (for default Magento 2 setting with the expiration of 1 day):

HTTP/1.1 200 OK
Date: Sun, 05 Jan 2020 19:36:08 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=6ho2jvaeuiio58qb29cbnpmvaf; expires=Sun, 05-Jan-2020 20:36:08 GMT; Max-Age=3600; path=/; domain=www.example.com; secure; HttpOnly
Pragma: cache
Cache-Control: max-age=86400, public, s-maxage=86400
Expires: Mon, 06 Jan 2020 19:36:08 GMT
X-Magento-Tags: store,cms_b,cms_b_mpanel_default_sidebar_block,cat_c,store_group,cat_c_54,cat_c_p_54,cat_p_149,cat_p,cat_p_81,cat_p_42,cat_p_41,cat_p_118,cat_p_116,cat_p_105,cat_p_89,cms_b_header_1_social_block,cms_b_header_1_top_left_link,cms_b_header_1_top_middle_block

Take note of Cache-Control: max-age=86400, public, s-maxage=86400 indicating one day expiration. This is the TTL of a page defining for how long it will be cached in Varnish.

In this way everything works in harmony:

  • Browsers do not cache website's HTML and thus never get stale content
  • Varnish caches for as long as one day, and you can safely up this value to reasonably longer time, e.g. 1 week. Because Magento can "talk" to Varnish to clear cache as needed (when product price has changed, for example).

Now, of course you can adjust VCL to emit something other than -1 for client. In this way browsers can cache things as well. But this is simply highly discouraged. Because there is no similiar mechanism (Magento->Varnish cache purging), which you can leverage in order to clear browser caches when you update a product's price. You simply can't get into those computers to clear the cache :)

How can I set expires to one week?

This task is performed in Magento admin settings.

Stores -> Configuration -> Advanced -> System -> Full Page Cache -> TTL for public content.

For one week you would set this setting to value 604800.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.