Read in 4 minutes

last updated 

Configure Magento 2 to use Varnish on CentOS 7

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.

Prerequisites

Make sure you have followed all instructions in 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 a 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 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 configuration which we created in the first post to handle SSL/TLS termination and as a back-end for Varnish.

/etc/nginx/conf.d/linuxize-test.com.conf
upstream fastcgi_backend {
  server   unix:/run/php-fpm/magento.sock;
}

server {
    listen 127.0.0.1:8080;
    server_name linuxize-test.com www.linuxize-test.com;

    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;
    server_name www.linuxize-test.com;

    ssl_certificate /etc/letsencrypt/live/linuxize-test.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/linuxize-test.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/linuxize-test.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://linuxize-test.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name linuxize-test.com;

    ssl_certificate /etc/letsencrypt/live/linuxize-test.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/linuxize-test.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/linuxize-test.com/chain.pem;
    include snippets/ssl.conf;

    access_log /var/log/nginx/linuxize-test.com-access.log;
    error_log /var/log/nginx/linuxize-test.com-error.log;

    location / {
        proxy_pass http://127.0.0.1;
        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:

/etc/nginx/nginx.conf

...
# 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
Advertisement

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:

/etc/varnish/default.vcl
...
.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:

/etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80

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

sudo systemctl enable varnish
sudo systemctl start varnish

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


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:

Configure Magento 2 to use Varnish on CentOS 7