How To Install and Configure Varnish Cache 5.2 with NGINX on CENTOS servers

Introduction

In this tutorial, we will cover how to install and configure Varnish with NGINX server to improve performance.

As explained on varnish-cache.org:

"Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture."

In this tutorial, we'll see how to setup a Varnish instance with default parameters. Remember to define and analyse the best setup for your goals (example: Magento).

Prerequisites

For this tutorial, we assume you already installed and configured an Nginx Web-Engine on a Linux distribution which will run on port 80. You can setup a new web server or install Varnish on your actual server.

All communications between Varnish and your Nginx server will be done using the private network. In this configuration, we'll also assume that your have a Load Balancer which will manage the HTTPS connection. On our private network, all connections will be done through 80 for Varnish and 8081 for NGINX.

You'll also need root or sudo accesses to run the container.

Lastly, check of course that Docker is installed, which is the default setup for servers at Artifakt.

Goal

We want to have the Varnish instance available for users and which will discuss with the NGINX server as a backend instance. 

Be sure you have all prerequisites checked to go further in this procedure.

Install Varnish as a Docker container

The easy way uses our own internal docker

docker run --name=varnish -p 80:80 --restart=always registry.artifakt.io/varnish:5

Injecting specific configuration

Depending on your use-case, the default configuration might not be enough. Varnish uses the Varnish Configuration Language format. It requires the presence of the configuration file on the host itself. All it takes is finally to inject the configuration file at container runtime:
docker run --name=varnish --restart=always -p 80:80 \
-v $VCL_PATH:/etc/varnish/default.vcl registry.artifakt.io/varnish:5
 
WARNING: if you already have a running Varnish container, you will need to remove it before starting a new one. Use this command for this purpose. Please be careful to schedule this because downtime will occur between stop and start steps.
docker rm -f varnish

Conclusion

As we've seen, it only takes a few minutes to enable a massive performance boost on your frontend servers. Varnish is a very powerful component, and easy to deploy too.
 
Feel free to contact Artifakt support for more advanced use cases, like High Availability or zero-downtime upgrades.
 
 
 
 

 

Was this article helpful?
0 out of 0 found this helpful