What is varnish cache

Introduction

Varnish is a reverse HTTP proxy, sometimes referred to as an HTTP accelerator or a web accelerator. A reverse proxy is a proxy server that appears to clients as an ordinary server. Varnish stores (caches) files or fragments of files in memory that are used to reduce the response time and network bandwidth consumption on future, equivalent requests. Varnish is designed for modern hardware, modern operating systems and modern workloads.

Varnish is more than a reverse HTTP proxy that caches content to speed up your server. Depending on the installation, Varnish can also be used as

  • Web application firewall,
  • DDoS attack defender,
  • Hot linking protector,
  • load balancer,
  • Integration point,
  • Single sign-on gateway,
  • Authentication and authorization policy mechanism,
  • Quick fix for unstable backbends, and
  • HTTP router.

Varnish topology diagram

The following figure shows a basic view of Varnish in your Magento topology.

In the preceding figure, users’ HTTP requests over the internet result in numerous requests for CSS, HTML, JavaScript, and images (referred to collectively as assets). Varnish sits in front of the web server and proxies these requests to the web server.

Process overview

This topic discusses how to initially install Varnish with a minimal set of parameters and test that it works. Then you’ll export a Varnish configuration from the Magento Admin and test it again.

The process can be summarized as follows:

  1. Install Varnish and test it by accessing any Magento page to see if you’re getting HTTP response headers that indicate Varnish is working.
  2. Install the Magento software and use the Magento Admin to create a Varnish configuration file.
  3. Replace your existing Varnish configuration file with the one generated by the Admin.
  4. Test everything again.

If there is nothing in your < your Magento install dir > /var/page_cache directory, you’ve successfully configured Varnish with Magento!

Install Varnish

Configure your web server

Configure your web server to listen on a port other than the default port 80 because Varnish responds directly to incoming HTTP requests, not the web server.

In the sections that follow, we use port 8080 as an example.

To change the Apache 2.2 listen port:

  1. Open /etc/httpd/conf/httpd.conf in a text editor.
  2. Locate the Listen directive.
  3. Change the value of the listen port to 8080. (You can use any available listen port.)
  4. Save your changes to httpd.conf and exit the text editor.

Modify the Varnish system configuration

To modify the Varnish system configuration:

Step 1: Open /etc/sysconfig/varnish (or /etc/default/varnish on Debian and Ubuntu) in a text editor.

Step 2: Set the Varnish listen port to 80.

Step 3: If necessary, comment out the following:

Step 4: Save your changes to /etc/sysconfig/varnish (or /etc/default/varnish on Debian and Ubuntu) and exit the text editor.

Modify default.vcl

This section discusses how to provide minimal configuration so Varnish returns HTTP response headers. This enables you to verify Varnish works before you configure Magento to use Varnish.

To minimally configure Varnish:

Step 1: Back up default.vcl:

Step 2: Open /etc/varnish/default.vcl in a text editor

Step 3: Locate the following stanza:

Step 4: Replace the value of .host with the fully qualified host name or IP address and listen port of the Varnish backend or origin server; that is, the server providing the content Varnish will accelerate.

Step 5: Replace the value of .port with the web server’s listen port (8080 in this example). Example: Apache is installed on host 192.0.2.55 and Apache is listening on port 8080:

Step 6: Save your changes to default.vcl and exit the text editor

Step 7: Restart Varnish:

If Varnish fails to start, try running it from the command line as follows:

Verify Varnish is working

The following sections discuss how you can verify that Varnish is working but without configuring Magento to use it. You should try this before you configure Magento.

Perform the tasks discussed in the following sections in the order shown:

  • Start varnish
  • Netstat
  • Enter

If Varnish fails to start as a service, start it from the command line as follows:

Step 1: Start the Varnish CLI:

Step 2: Start the Varnish child process:

When prompted, enter start

The following messages display to confirm a successful start:

 

Step 2: Start the Varnish child process:

When prompted, enter start

The following messages display to confirm a successful start:

Net stat

Log in to the Varnish server and enter the following command:

Look for the following output in particular:

The preceding shows Varnish running on port 80 and Apache running on port 8080.

If you don’t see output for varnishd, make sure Varnish is running.

Look at the Varnish log

Configure Magento to use Varnish

To configure Magento to use Varnish:

  1. Log in to the Magento Admin as an administrator.
  2. Click STORES > Configuration > ADVANCED > System > Full Page Cache
  3. From the Caching Application list, click Varnish Caching
  4. Enter a value in the TTL for public content field.
  5. Expand Varnish Configuration and enter the following information:
  6. Click Save Config.
  7. Click one of the export buttons to create a default.vcl you can use with Varnish. For example, if you have Varnish 4, click Export VCL for Varnish 4

    The following figure shows an example.

  8. Replace your existing default.vcl with the one you just exported.
  9. We recommend you open default.vcl and change the value of acl purge to the IP address of the Varnish host. (You can specify multiple hosts on separate lines or you can use CIDR notation as well.)

    For example,

  10. Restart Varnish and your web server:

Check page load times

If Varnish is working, 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:

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

    The page load time displays next to the page URL. In this case, the load time is 5 ms. This helps confirm that Varnish cached the page.

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