Varnish Cache is an open-source, high-performance HTTP accelerator designed for speeding up web servers.
In short, this blog will show you how to install and use Varnish Cache as a front-end to the Nginx HTTP server in CentOS 7. Need any assistance regarding installation and configuration, you can take our technical team support to been done on your behalf under our server management services
Step 1
Install Nginx Web Server on CentOS 7
By using the YUM package manager you can install the Nginx HTTP server from the default CentOS repositories.
# yum install nginx
Install Nginx on CentOS 7
Once done with the installation, start enabling the Nginx service automatically at system boot.
# systemctl start nginx
# systemctl enable nginx
# systemctl status nginx
Start and Enable Nginx at Boot
By using the following commands to modify system firewall rules to permit inbound packets on port 80.
# firewall-cmd --zone=public --permanent --add-port=80/tcp
# firewall-cmd --reload
# firewall-cmd --zone=public --permanent --add-port=8080/tcp
# firewall-cmd –reload
Step 2
Ins
Install Varnish Cache on CentOS 7
For the varnish cache 6 latest version
The pre-compiled RPM packages available for the latest version of Varnish Cache 6 (i.e 6.5 at the time of writing), therefore you are requested to add the official Varnish Cache repository.
Enable the EPEL repository in order to install several dependency packages.
# yum install -y epel-release
Next, install pygpgme, the package that handles the signatures and yum-utils, a collection of useful utilities that extend yum’s native features in various ways.
# yum install pygpgme yum-utils
Create a file named /etc/yum.repos.d/varnishcache_varnish5.repo that contains the repository configuration.
# vi /etc/yum.repos.d/varnishcache_varnish65.repo
Important: Ensure to replace el and 7 in the config below with your Linux distribution and version:
[varnishcache_varnish65]
name=varnishcache_varnish65
baseurl=https://packagecloud.io/varnishcache/varnish65/el/7/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish65/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
Update local yum cache and then install varnish cache package by running the command.
# yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish65'
# yum install varnish
Once the varnish cache has been installed, and the main executable will be installed as /usr/sbin/varnishd and varnish configuration files are located in /etc/varnish/:
- /etc/varnish/default.vcl – this is the main varnish configuration file, it has been written by using vanish configuration language(VCL).
Now start the varnish service, by enabling automatically start during system boot, ensure it’s running well by verifying the status.
# systemctl start varnish
# systemctl enable varnish
# systemctl status varnish
Confirm that varnish has successfully installed on your system.
$ which varnishd
$ varnishd -V
Sample Output
varnishd (varnish-6.5.1 revision 1dae23376bb5ea7a6b8e9e4b9ed95cdc9469fb64)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2020 Varnish Software
Step 3
Configure Nginx to Work With Varnish Cache
Configure Nginx to work with Varnish Cache, where you can see the Nginx listens on port 80 as default. later, you need to change the default Nginx port to 8080.
Open the Nginx config file /etc/nginx/nginx.conf and find default listen 80 and change it to 8080 in the server block.
# vi /etc/nginx/nginx.conf
Note: Do it in all server block configuration files (usually created under /etc/nginx/conf.d/) for websites that serve under the varnish.
Later, find the parameter ExecStart by opening the varnish service configuration file that displaces the port and then tries to change the port from 6081 to 80.
# systemctl edit --full varnish
The line displaces as shown below.
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
Next, set up Nginx as a backend server for Varnish proxy, in the /etc/varnish/default.vcl configuration file.
# vi /etc/varnish/default.vcl
Define host IP and port from the backend section
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Set this to point to your actual content server.
Restart Ngnix HTTPD once all the necessary configuration has been done. You can see the above changes.
# systemctl daemon-reload
# systemctl restart nginx
# systemctl restart varnish
Step 4
Test Varnish Cache on Nginx
Finally, test the varnish cache is enabled and working well with the Nginx service by using the following command.
# curl -I http://localhost
Output
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 06 Jan 2021 09:24:18 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
ETag: "53762af0-12e1"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.5)
Accept-Ranges: bytes
Connection: keep-alive
Conclusion
This blog will explain to you how to install and setup varnish cache for the Nginx HTTP server on centOS 7. If you need any assistance, our technical team will be available at any time to help you.