Cache Management for Beginner | How to enable Varnish Cache in Magento2 | Part4

How to enable Varnish Cache in Magento2:

What is Varnish : Varnish Cache is an open source web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents.

Where varnish will store data :

  • RAM (memory) – with the malloc storage config
  • Disk – with the file storage config

How to Install Varnish: Execute following commands to install varnish in your server. (depend on the OS, command may change)

Where to get Varnish Configuration File Path:

  • CentOS 6: /etc/sysconfig/varnish
  • CentOS 7: /etc/varnish/varnish.params
  • Debian: /etc/default/varnish
  • Ubuntu: /etc/default/varnish

Basic Varnish Configuration file Change:

  • Set the Varnish listen port to 80: VARNISH_LISTEN_PORT=80
  • Below change is required only for Varnish 4.x

DAEMON_OPTS=”-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m”

  • Save conf file.
  • Download existing VCL file Replace /etc/varnish/default.vcl file with vcl or you can also modify the VCL file
  • If manually your editing the VCL file , than add following change

backend default { .host = “127.0.0.1”; .port = “8080”; .first_byte_timeout = 600s; }

  • Start / Restart Varnish : service varnish start (or) Sudo service varnish restart

Refer : https://devdocs.magento.com/guides/v2.3/config-guide/varnish/config-varnish-configure.html

Magento admin Changes for Varnish :

  1. Go to Admin Panel > Store > Configuration > Advance > System > Full page cache
  2. Change the field built-in-cache to varnish cache.
  3. Enter a value in the TTL for public content field.
  4. Expand Varnish Configuration and fill it necessary information.
  5. Save configuration

Run Following commands in magento root :

  • php bin/magento cache:flush
  • rm -rf var/cache/* var/page_cache/*

How to verify Varnish is working or not:

  • Verify HTTP response headers
  • root> curl -I -v –location-trusted ‘<Magento URL>’

Header Must be :

X-Magento-Cache-Control: max-age=86400, public, s-maxage=86400

Age: 0

X-Magento-Cache-Debug: MISS

(Or)

Once after above steps are done then load your website  Go to Network tab in Inspect Element page execution time will be less compare to before Varnish. Cross verify with header.Also var/page_cache folder should be empty.

Note: Varnish does not support HTTPS (SSL protocol). As a workaround, there is an option to configure an SSL terminator or proxy to decrypt HTTPS traffic from visitors before it reaches Varnish and encrypt the response back. Magento 2 supports Varnish 4.x and 5.x

Save WATER to Save LIFE on Earth.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s