How to Configure Varnish for Magento 2

- Magento 2

Magento 2 Varnish Guide

Benchmarks show that Magento 2 is much faster than the 1.X version of the platform, but you can improve its performance with the aid of Varnish. Being an HTTP accelerator, it is often used with heavily consumed APIs and content-heavy dynamic websites. Unlike other similar solutions, Varnish is focused exclusively on HTTP. About 10% of the Top 10K websites rely on this accelerator. The most prominent Varnish users are The New York Times, Wikipedia, Vimeo, Facebook, and Twitter. Since the performance of these websites might be a great reason to use Varnish with Magento 2, we’d like to shed light on its configuration within the platform, but let’s start with the nature of the accelerator.

Every web server receives HTTP requests and returns HTTP responses.  Unfortunately, it doesn’t return the response immediately. A long sequence of steps required for each request may include loading script files, launching interpreter processes, and performing database queries. When all these actions are multiplied by thousands of requests, the server gets overloaded, system resources are drained, and you witness a significant performance decrease.

It is also worth mentioning that there are a lot of repeated requests, but the server does not remember them, so the same painful processes take place. Luckily, you can change this algorithm by using Varnish. In case of this HTTP accelerator, every request is received by Varnish instead of your web server. It looks at what’s being requested and sends the request to its backend, which is your web server. In its turn, the server performs its regular tasks and sends a response to Varnish to pass it back to the client.

The increase of performance occurs due to Varnish’s ability to store responses from the back end. All the responses are situated in its cache, so Varnish can rapidly serve further repeated responses without connecting to the back-end server.

So, by using Varnish, you get the following benefits:

  • the load on the back end is reduced;
  • the time of nearly every response is improved;
  • the system can serve more requests per second.

Another optimization that makes Varnish lightning fast is in-memory cache storage. Please note that memory is more limited than disk, so you it is extremely necessary to follow these two rules: size your Varnish cache properly; prevent the system from caching duplicate objects.

Now, when you know why Varnish is so valuable, we can continue with its configuration within Magento 2. The platform supports versions 3.0.5 or later or any Varnish 4.x version out-of-the-box, so you will not spend too much time and effort on its installation and maintenance.

Please pay attention to the following aspects:

  • Enter the commands described below as a user with root privileges. Note that there can be exceptions.
  • The post illustrates Varnish configuration on CentOS and Apache 2.2. In other environments, some commands can be different, so use the official Varnish documentation.

Magento 2 Varnish issues

  • The lack of SSL support (but you can fix this problem with the help of SSL termination proxy).
  • It is necessary to restart Varnish every time you manually delete the contents of your <your Magento install dir>/var/cache directory.
  • For the following error:

Edit default.vcl by adding a timeout to the backend stanza:

Check if Magento 2 supports your Varnish version

To display a current Varnish version, use the following command:

You should get the following result:

Your Varnish should not be older than 3.0.5 or it can be any version of 4.x.

Configure your web server for using Varnish

Now, when you know that you use a suitable Varnish version, it is time to configure your web server. It should replace the default port 80, since Varnish responds directly to incoming HTTP requests. In our example, we use port 8080. Let’s change the listen port of Apache 2.2:

  1. Find /etc/httpd/conf/httpd.conf and open it in a text editor.
  2. Now, you should locate the Listen directive and change the value of the listen port to 8080 or any other available listen port.
  3. Save the changes and close the editor.

Change the Varnish system configuration

The next step requires modifying the Varnish system configuration:

1. Find /etc/sysconfig/varnish and open it in a text editor.

2. Now you should set the Varnish listen port to 80 VARNISH_LISTEN_PORT=80

3. Please note that it might be necessary to comment out the following:

4. Save the changes and close the editor.

Change default.vcl

The below material describes how to set Varnish to return HTTP response headers. As a result, you will be able to verify that  Varnish works even before you configure Magento 2 for using Varnish.

1. Create the backup of default.vcl:

2. Take your /etc/varnish/default.vcl file and open it in a text editor.

3. Find the following code:

4. Now, you have to replace the value of .host with IP address and listen port of the Varnish backend or the server that provides Varnish with the content for acceleration. Probably, it should be your web server. For further information, follow this link.

5. Replace the value of .port with 8080 or any other web server’s listen port. In our example, Apache is installed on host

In a situation when Apache and Varnish are running on the same host, it is better to use an IP address or hostname instead of localhost.

6. Save the changes and close the editor.

7. Now, you can restart Apache. Use the following command:

8. The same is about Varnish. Run the command below to restart it:

In case of failed start, run Varnish from the command line:

This should lead to the appearance of error messages. For further help check these sources: flatlinesecurityCentOS wiki; and CentOS documentation.

Verify that Varnish is working without configuring Magento 2

Perform the following tasks before configuring Magento 2. Thus, you will be able to find out problems related to Varnish on early stages.

Start Varnish

The easiest way to start Varnish is to enter the service varnish start command. If the accelerator fails to run as a service, you can easily start it from the command line:

1. First of all, you have to start the Varnish CLI:

2. The second step requires starting the Varnish child process. Then, you will be asked to enter start. The message below illustrates a successful start:


Now, it’s time to log in to the Varnish server and run this command:

You should get output similar to the following one:

From the above example, we can see that Varnish runs on port 80, while Apache – on port 8080. If there is no output for varnishd, check if Varnish is running. For further information about netstat options, follow this link.

Install Magento 2

Install Magento 2 if it is not installed yet. As for Varnish configurations, use the Varnish host and port 80 when prompted for a Base URL.

In case of the following error:

edit the default.vcl file by adding a timeout to the backend stanza:

Check if Varnish looks at HTTP response headers

To verify if Varnish is serving pages by looking at returned HTML response headers set Magento 2 for developer mode. Use the magento deploy:mode:set command or modify your .htaccess file. The second method is described below:

1. Be logged in to the Magento server as the Magento file system owner.

2. Find the <your Magento install dir>/.htaccess file and open it in a text editor.

3. Uncomment this line: SetEnv MAGE_MODE developer.

4. Save the changes and close the editor.

Now, it is necessary to look at the Varnish log. If Varnish is running, enter the following command on its server:

If not – start Varnish and run the same command. Open a web browser and go to any page of your Magento 2.

There will be a long list of response headers in a command prompt window. Find the following:

If there are no such headers, stop Varnish, look if everything is ok with your default.vcl, and try again.

And finally we can examine HTML response headers. You can use Live HTTP Headers – a browser plug-in for Firefox and Chrome. In this post, we rely on curl. Enter the command from any machine that has access to the Magento server via HTTP:

For curl -I -v –location-trusted ‘’, you will get the following result:

Configure Magento 2 to use Varnish

To configure Magento 2 to use Varnish  You should be logged in as a Magento administrator.

1. Go to STORES > Configuration > ADVANCED > System > Full Page Cache

2. Find Varnish Caching in the Caching Application list. Choose this option.

3. In TTL for public content, enter a value.

4. Expand Varnish Configuration with the following information:

  • Access list requires the fully qualified host name, IP address, or CIDR for which to invalidate content. Follow this link for further information.
  • Backend host requires the fully qualified hostname or IP address as well as listen port of the Varnish backend (origin server). It can be your web server that provides the content for Varnish to accelerate. Follow this link for additional information.
  • Backend port requires backend server’s listen port.

5. Don’t forget to save configurations. Click Save Config.

6. Now, it is necessary to create a default.vcl file that you will use with Varnish. Use one of the export buttons.

7. For Varnish 4, use Export VCL for Varnish 4.

Magento 2 VCLExport

8. Utilize this file to replace your old default.vcl.

9. The last thing you should do is to relaunch both Varnish and a web server:

Final steps

First of all, you should Verify HTTP response headers once more.

For curl, we have the following example:

Note that important headers are

X-Magento-Cache-Debug: HIT is also acceptable.

Now, it is necessary to check page load times. Note that for front door, storefront category, or any Magento 2 page it should be less than 150ms. You can use a browser inspector to measure index within your store. The algorithm for Chrome inspector is described below:

1. In Chrome, open any cacheable page of your Magento 2.

2. Open the pop-up menu right-clicking anywhere on the page.

3. Choose the Inspect Element option.

4. Now, while in the inspector pane, you have to click the Network tab.

5. Don’t forget to refresh the page.

6. Scroll to the top of the inspector pane. You should see the URL of the Magento 2 page you are viewing. The page load time is displayed next to the page URL.

7. By clicking the page URL in the Name column, you will see HTTP response headers.

Finally, you can verify the Magento 2 cache.

The <your Magento install dir>/var/page_cache directory should be empty. You should be logged in to your Magento 2 server as the Magento 2 file system owner.

1. To verify Magento 2 cache, run this command:

2. Access cacheable Magento 2 pages.

3. The var/page_cache/ directory should be empty. It means that Varnish for Magento 2 is configured successfully.

Check the official Magento 2 Varnish guide and Magento 2 Demo.

Mirasvit Full Page Cache Warmer for Magento 2

We also recommend you to pay attention to the Mirasvit Full Page Cache Warmer Magento 2 extension. This tool allows you to increase the speed of your store pages – loading speed is increased up to several times. Besides, you get more effective usage of FPC and a robust background functionality when store pages are improved without loading speed issues. The latest cache version and the fastest page speed are guaranteed! Consequently, you increased SERP ranking and get more loyal customers with all that it implies.

All pages processed by the extension are gathered in a grid:

Mirasvit Full Page Cache Warmer Magento 2 Extension Review

And you can easily choose what pages should be warmed:

Mirasvit Full Page Cache Warmer Magento 2 Extension Review

Besides, it is possible to run cron job:

Mirasvit Full Page Cache Warmer Magento 2 Extension Review

Check our review on the extension here: Mirasvit FPC Warmer Overview.

Download / Buy Mirasvit Full Page Cache Warmer Magento 2 Extension