How to Install, Configure, and Use Varnish in Magento 2.

Varnish

Are you considering getting rid of Magento Default Cache? Unsure about setting up Varnish or tired of reading related blogs? This blog has got you covered😊.

Hello Everyone! This is MO😉. In this blog, I will explain how to install and configure Varnish on the same server or on different servers. So, without any delay, let's get started. 

If you want to improve your website's performance, it's important to understand the different types of Magento caches. Check out our blog for more information. "Demystifying Magento2 Caches: Optimizing Your Store for Speed and Performance".

Varnish

Varnish – open-source cache technique for web applications. Also known as HTTP accelerator or caching HTTP reverses proxy. Varnish will reduce the response time and network bandwidth consumption on future, equivalent requests. Unlike web servers like Apache and Nginx, Varnish was designed exclusively with the HTTP protocol.

varnish

Here we are using the below versions of the required packages.

Magento Varnish

Install Varnish

https://www.tecmint.com/install-varnish-cache-web-accelerator/

For rpm-based installations, refer to official documentation

https://varnish-cache.org/docs/

After that, you will see the Varnish version below if everything is correct.

varnished -V

The minimum recommended RAM allocation for varnish is 4 GB.

Configurations

Now we will modify Varnish System Configuration, to make Varnish run

on port 80 rather than the default port.

Change default port configuration at /etc/systemd/system/varnish.service

Now we will start varnish

Systemctl daemon-reload

systemctl start varnish

# systemctl status varnish

# system enable varnish

Varnish Configuration with Magento 2

Case 1: Varnish on the same host as a web server

After installing Varnish, you need to configure the current web server.

We will configure our web server to listen on port 8080 other than the

default port 80.

Because now varnish will listen on port 80.

Change webserver configuration

Change varnish configuration at /etc/varnish/default.vcl

# Default backend definition. Set this to point to your content server.

backend default {

.host = "127.0.0.1";

.port = "8080";

}

Since our web server is on the same host and running on port 8080.

Now, we need to Configure Magento to use Varnish

Please go to Stores > Configuration > Advanced > System > Full Page Cache,

Export VCL for varnish

We can use vcl file generated from Magento as well, but make changes

as above to change the backend host and port.

Case 2: Varnish on a different host as a web server

Varnish server: xxx.xxx.xxx.xxx port 80

Magento server: yyy.yyy.yyy.yyy port 80

Changes need to be made on the varnish server:

  • login to the varnish server
  • go to file /etc/varnish/default.vcl
  • Under the "backend default" update

.host = "yyy.yyy.yyy.yyy";//(use Magento server IP for better network)

.port = "80";//(Magento web server port)

  • Restart the Varnish (systemctl restart varnish)

Note: Kindly use the default VCL that is generated during the varnish
installation and

Don't update it with Magento-generated VCL for varnish ( available from

Magento Admin)

Changes need to be made on the Magento server:

  • Log in to the Magento server
  • Go to the env.php file located in the app/etc directory
  • Update the values in

'http_cache_hosts' => [

[

'host' => 'xxx.xxx.xxx.xxx', //(varnish server public ip)

'port' => '80' // ( varnish server port)

]

]

Now update your base URLs on the core_config_data table to your

varnish public Ip (http://xxx.xxx.xxx.xxx/)

flush the Magento caches ( bin/magento cache:flush)

How to Verify Varnish in Magento 2

Case 1: Verify HTTP Response Headers

Curl or another utility can be used to view HTTP response headers on

Magento pages.

You won't be able to see the headers unless you are using Magento's

developer mode.

To Enable developer mode:

bin/magento deploy:mode:set developer

For example,

curl -I -v --location-trusted 'http://192.0.2.55/magento2'

Important headers:

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

Age: 0

X-Magento-Cache-Debug: MISS

Note: This value is also acceptable: X-Magento-Cache-Debug: HIT.

Case 2: Check load time

If Varnish works, any Magento page with cacheable blocks should

load in less than 150ms. Examples of such pages are the front door and

storefront category pages.

Use a browser inspector to measure page load times.

For example, to use the Chrome inspector:

  • Access any cacheable Magento page in Chrome.
  • Right-click anywhere on the page.
  • From the pop-up menu, click Inspect Element
  • In the inspector pane, click the Network tab.
  • Refresh the page.
  • Scroll to the top of the inspector pane to see the URL of the page you are viewing.

The following figure shows an example of loading the Magento2 index

page. 

config_varnish_inspector

The page load time is displayed next to the page URL. In this case, the

load time is 5 ms. This helps confirm that Varnish cached the page.

  • To view HTTP response headers, click the page URL (in the Name column).

You can view HTTP headers which are discussed in more detail in Verify

HTTP response headers.

Case 3: Verify the Magento cache

Make sure the <magento_root>/var/page_cache directory is empty:

  • Log in to your Magento server, or switch to, the file system owner.
  • Enter the following command:

rm -rf <magento_root>/var/page_cache/*

  • Access one or more cacheable Magento pages.
  • Check the var/page_cache/ directory.

If the directory is empty, congratulations! You successfully configured

Varnish and Magento to work together!

  • If you cleared the var/page_cache/ directory, restart Varnish.

FAQ's 

What is Varnish in the context of Magento 2? 

Varnish is a popular caching technology that can be used with Magento 2 to improve the performance and scalability of an online store. It acts as a reverse proxy, caching the HTML responses from the server and serving them directly to visitors, reducing the load on the Magento server.


How does Varnish improve performance in Magento 2? 

Varnish improves performance by caching the HTML responses generated by the Magento server. When a user requests a page, Varnish checks if it has a cached copy of the page and serves it directly, avoiding the need for the server to generate the page again. This reduces the server load and improves response times, leading to faster page loading for users.


What are the benefits of using Varnish in Magento 2? 

Using Varnish in Magento 2 provides several benefits, including:
  • Improved performance and faster page loading times for users.
  • Reduced server load and better scalability for handling high traffic loads.
  • Better handling of concurrent user requests.
  • Caching of static and dynamic content, leading to improved overall site performance.
  • Increased resilience to traffic spikes and improved availability.


Are there any limitations or considerations when using Varnish in Magento 2? 

While Varnish provides significant performance improvements, there are some considerations to keep in mind:
  • Proper Varnish configuration is important, and it might require expertise or developer assistance.
  • Varnish may not be suitable for websites with highly personalized or dynamic content.
  • Care should be taken when implementing custom functionalities or personalized experiences, as they may not work well with Varnish caching.
  • Extensions or customizations that rely on cookies or session-specific data might require additional configuration to work properly with Varnish.


Can Varnish cache all types of pages in Magento 2? 

By default, Varnish can cache most static or publicly cacheable pages in Magento 2, such as product pages, category pages, and CMS pages. However, certain pages that require authentication or contain user-specific information are not generally cacheable, and Varnish might bypass caching for such pages. Customizations or extensions might be required to make specific pages cacheable.

How can I clear or purge the Varnish cache in Magento 2? 

To clear the Varnish cache in Magento 2, you can use various methods:
  • Using the Varnish administrative interface or command-line tools.
  • Sending a PURGE request from the Magento backend or using a REST API.
  • Utilizing cache clearing extensions available in the Magento Marketplace.
  • Preconfiguring cache tags in Magento so that Varnish will automatically clear specific pages when related content is updated.

Thank you for Reading. Are you fascinated to know more about Magento, What are you waiting for? Check out the other blogs in the Magento section and follow us through Youtube and Instagram to know more about us.

Post a Comment

0 Comments