Varnish is a popular, open source, http accelerator that allows you to easily speed up content heavy websites. It works with popular web server such as Apache and NGINX. In this article, we will look at how to install Varnish on CentOS 7 for NGINX.
How to Install Varnish on CentOS 7 for NGINX
Here are the steps to install, setup and test Varnish accelerator with NGINX web server. We will be installing Varnish 6.5. You may change it as per your requirement.
1. Install NGINX
If you have already installed NGINX on your system, you can skip this step. Else open terminal and run the following commands to install NGINX on your system.
# yum install nginx
Once NGINX is installed run the following commands to start it, and enable it to auto start during system reboot.
# systemctl start nginx # systemctl enable nginx # systemctl status nginx
If you have not modified the firewall rules for port 80, you can do so with the following commands.
# firewall-cmd --zone=public --permanent --add-port=80/tcp # firewall-cmd --reload # firewall-cmd --zone=public --permanent --add-port=8080/tcp # firewall-cmd --reload
2. Install Varnish
First you need to add the official Varnish cache repository.
# yum install -y epel-release
Next, install prerequisites such as pygpgme to process GPG signatures and yum-utils for additional features in yum package manager.
# yum install pygpgme yum-utils
Create repo configuration file for Varnish 6.5.
# vi /etc/yum.repos.d/varnishcache_varnish65.repo
Add the following lines to it. Replace el and 7 with your Linux distribution and version respectively.
[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 [varnishcache_varnish65-source] name=varnishcache_varnish65-source baseurl=https://packagecloud.io/varnishcache/varnish65/el/7/SRPMS 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
Run the following command to update local yum package and install Varnish cache. If you see a prompt asking you to accept GPG key, enter y and proceed further.
# yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish65' # yum install varnish
Once Varnish is installed, you will find the main executable at /usr/sbin/varnishd and its configuration file at /etc/varnish/
Run the following commands to run Varnish as well as enable it to autostart during system boot.
# systemctl start varnish # systemctl enable varnish # systemctl status varnish
3. Configure Varnish for NGINX
Open NGINX configuration file in a text editor.
# vi /etc/nginx/nginx.conf
By default, NGINX runs on port 80. When you use Varnish, it needs to run on port 80 and NGINX will need to run on another port such as 8080, behind it.
So replace the following line.
listen 80;
with
listen 8080;
It will look like
server { listen 8080; ... }
You need to do this for all server blocks that you want to run behind Varnish.
Next, run the following command to open Varnish service configuration file, and change the default port of Varnish from 6081 to 80.
# systemctl edit --full varnish
The ExecStart line should look like the following with port 80 (in bold)
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
Save and close the file.
At this point, we have configured NGINX to run on port 8080 and Varnish to run on port 80. Now, we need to configure Varnish to pass the incoming requests to NGINX port 8080. For that, open default configuration file of Varnish.
# vi /etc/varnish/default.vcl
Look for backend section and change the port value to 8080.
backend default { .host = "127.0.0.1"; .port = "8080"; }
Save and close the file. Run the following commands to restart NGINX and Varnish.
# systemctl daemon-reload # systemctl restart nginx # systemctl restart varnish
4. Test Varnish Configuration
Open terminal and send a request to port 80 where Varnish is listening.
# curl -I http://localhost
You will see a similar output as shown below.
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
You will see a couple of headers mention Varnish above, indicating that the request is received and processed by Varnish cache. Also, the server header displays NGINX indicating that the request was sent from Varnish to NGINX, which sent a response back to Varnish, which was passed on to you.
In this article, we have learnt how to install, setup & test Varnish cache with NGINX server in CentOS system. You can use these steps for RHEL as well as Fedora systems.
Also read:
How to Convert JPG to PDF in Linux
How to Install & Configure ElasticSearch in Linux
How to Use Strace Command in Linux
Top SFTP Command Examples
How to Clear Cache Memory in Linux