1. Home
  2. Linux
  3. CentOS
  4. Configure Magento 2 to use Varnish on CentOS 7

Configure Magento 2 to use Varnish on CentOS 7

Page Speed and Load Time

Want to learn how to install Magento 2 to use Varnish CentOS, Let’s have detailed tutorial below;

  • Page speed and load time are extremely important to the success of your online store.
  • Loading time is the total amount of time the website needs for the content on a specific page to load.
  • The longer the loading time is, the lower the conversion rate.
  • This is also one of the most important factors that Google considers when determining its search engine rankings.

One of the factors is Best Hosting Speed, so check out and try Best VPS Hosting  and WordPress Hosting.

install  Magento 2  to Varnish CentOS 7

In the first post,

  • we will teach you how to install  Magento 2 on CentOS 7 machine.
  • In the second post of this series, we are going to cover installing and configuring Varnish to make our Magento store super quick.


Be sure to check that you have followed every instruction in the first post and that you have EPEL repository enabled.

How it works

Varnish doesn’t support SSL, so we have to use a different service as an SSL Termination Proxy. In our situation, this will be Nginx.


  • Once 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.

Varnish and HTTPS

  • If it is cached, Varnish will return the cached data to Nginx without a request to the Magento application.
  • And if the request is not cached, Varnish will pass the request to Nginx on port 8080, which is going to pull the data from Magento so that Varnish can cache the response.
  • When visitors navigate to your website without SSL on port 80, then they are going to be redirected to the HTTPS URL on port 443 by Varnish.

Configuring Nginx

We have to modify the Nginx configuration which we made in the first post to handle SSL/TLS termination and have it act as a back-end for Varnish.

Content of the File:
upstream fastcgi_backend {
  server   unix:/run/php-fpm/magento.sock;
server {
    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_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 have to delete the default Nginx server block from the nginx.conf file. Comment or remove the lines below:

# 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 these changes to take effect:

sudo systemctl reload nginx

Installing and Configuring Varnish

  1. Varnish is a fast reverse-proxy HTTP accelerator that will sit in front of our web server and be used as a full page Cache solution for our Magento installation.
  2. 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 has to be run as root or a user with sudo privileges, and
  • it should create a file /etc/varnish/default.vcl using default values, which are to have localhost as back-end host and port 8080 as back-end port.

Default Configuration

  1. The default configuration comes with an incorrect URL for the health check file.
  2. Open the default.vcl file and remove the /pub section 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, but we need to change this to 80:


After 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 to perform debugging on Varnish.

Final Thoughts

That’s all for this tutorial. If you run into any problems, leave a comment below. In the next part of the series:
we will look into how to integrate Redis cache to our Magento store.
Here, we have picked the best tutorial guides that you  must read next:

  1. How to install Magento on cPanel
  2. How to Choose Your CMS?


Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']