Configure Magento 2 to use Varnish on CentOS 7

Updated on

4 min read

Magento 2 with Varnish

The page speed or loading time is crucial to the success of your online store. The loading time is the total amount of time it takes the content on a specific page to load. The longer the loading time is, the lower the conversion rate. It is also one of the most important factors that Google considers to determine the search engine rankings.

In the first post, we installed Magento 2 on our CentOS 7 machine. In the second post of this series , we will cover installing and configuring Varnish to make our Magento store super fast.


Make sure you have followed the instructions from the first post and you have EPEL repository enabled.

How it works

Varnish does not support SSL, so we need to use another service as an SSL Termination Proxy, in our case that will be Nginx .

When a visitor opens your website over HTTPS on port 443 the request will be handled by Nginx which works as a proxy and passes the request to Varnish (on port 80). Varnish checks if the request is cached or not. If it is cached, Varnish will return the cached data to Nginx without a request to the Magento application. If the request is not cached Varnish will pass the request to Nginx on port 8080 which will pull data from Magento and Varnish will cache the response.

If a visitor opens your website without SSL on port 80 then he will be redirected to the HTTPS on port 443 URL by Varnish.

Configuring Nginx

We need to edit the Nginx server block which we created in the first post to handle SSL/TLS termination and as a back-end for Varnish.

upstream fastcgi_backend {
  server   unix:/run/php-fpm/magento.sock;

server {

    set $MAGE_ROOT /opt/magento/public_html;
    set $MAGE_MODE developer; # or production

    include snippets/letsencrypt.conf;
    include /opt/magento/public_html/nginx.conf.sample;

server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;

    return 301$request_uri;

server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Port 443;

We also need to remove the default Nginx server block from the nginx.conf file. Comment or delete the following lines:

# server {
#     listen       80 default_server;
#     listen       [::]:80 default_server;
#     server_name  _;
#     root         /usr/share/nginx/html;
#     # Load configuration files for the default server block.
#     include /etc/nginx/default.d/*.conf;
#     location / {
#     }
#     error_page 404 /404.html;
#        location = /40x.html {
#     }
#     error_page 500 502 503 504 /50x.html;
#         location = /50x.html {
#     }
# }

Reload the Nginx service for changes to take effect:

sudo systemctl reload nginx

Installing and Configuring Varnish

Varnish is a fast reverse-proxy HTTP accelerator that will sit in front of our web server and it will be used as a Full Page Cache solution for our Magento installation.

Install Varnish via yum with the following command:

sudo yum install varnish

To configure Magento to use Varnish run:

php /opt/magento/public_html/bin/magento config:set --scope=default --scope-code=0 system/full_page_cache/caching_application 2

Next, we need to generate a Varnish configuration file:

sudo php /opt/magento/public_html/bin/magento varnish:vcl:generate > /etc/varnish/default.vcl

The command above needs to be run as a root or user with sudo privileges and it will create a file /etc/varnish/default.vcl using the default values which are localhost as back-end host and port 8080 as back-end port.

The default configuration comes with a wrong URL for the health check file. Open the default.vcl file and remove the /pub part from the line highlighted in yellow:

.probe = {
     # .url = "/pub/health_check.php";
     .url = "/health_check.php";
     .timeout = 2s;
     .interval = 5s;
     .window = 10;
     .threshold = 5;

By default, Varnish listens on port 6081, and we need to change it to 80:


Once you are done with the modifications, start and enable the Varnish service:

sudo systemctl enable varnishsudo systemctl start varnish

You can use the varnishlog tool to view real-time web requests and for debugging Varnish.


In this tutorial, we have shown you how to speed up your Magento instance by implementing Varnish as a full page cache.

If you hit any problems, leave a comment below.

This post is a part of the How to install and configure Magento 2 on CentOS 7 series.
Other posts in this series: