Linux Commands and Scripts

How to Install Varnish Cache for Nginx in CentOS 7

In this article, we will learn to install Varnish cache for Nginx in CentOS 7

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. Varnish is as fast as delivery 20 Gbps on regular off-the-shelf hardware. The performance of the Varnish cache is the flexibility of its configuration language. Its enables you to write policies on how incoming requests should be handled.VCL enables you to write policies on how incoming requests should be handled. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture.

Keep the server upto date

# yum update -y

Install Nginx web server.

# yum install nginx -y

After successfully installed Nginx, start and enable it.

# systemctl start nginx && systemctl enable nginx

Verify the nginx is running

# systemctl status nginx

Check the web port of the nginx

# netstat -pnltu

Before we install Varnish Cache, we need to enable the EPEL repository

# yum install -y epel-release

Install Varnish Cache

# yum install varnish -y

After successfully installed Varnish Cache, all configuration files will store in /etc/varnish

  • /etc/varnish/varnish.params - Varnish environment configuration.
  • /etc/varnish/default.vcl - This is default configuration file for Varnish.
  • /etc/varnish/secret - varnish secret file.

Now, start and enable varnish

# systemctl start varnish && systemctl enable varnish

Varify the service is successfully runing.

# systemctl status varnish

Check the executable path

# which varnishd

Check the version of the Varnish Cache

# varnishd -V

varnish cache version hostnextra

Configure Varnish for Nginx

Because Varnish cache sits in front of the webserver to follow HTTP requests, we need to replace the default Nginx port to port 8080, so it will run after Varnish caching.

Open the Nginx configuration file

# vi /etc/nginx/nginx.conf

Find server section and modify listening port

listen 8080 default_server;

install varnish cache in cenos

Restart the nginx service

# systemctl restart nginx

Now, setup Nginx as a backend server for Varnish, in the /etc/varnish/default.vcl configuration file.

# vi /etc/varnish/default.vcl

Find backend section and modify it as shown below

backend default {
.host = "127.0.0.1";
.port = "8080";
}

Next, modify the Varnish listening port to 80 in the Varnish environment config file

# vi /etc/varnish/varnish.params

Change varnish listen port to VARNISH_LISTEN_PORT=80

Now, everything is set, restart the varnish service.

# systemctl restart varnish

Verify if Varish is working with Nginx server

# curl -I http://localhost

varnish cache verify hostnextra

Today, we’ve learned how our Support Engineers Install Varnish Cache for Nginx in CentOS.

[Need assistance to fix this error or install tools? We’ll help you.]

Related Articles