How to Install and Setup Varnish Cache 6 with Nginx on CentOS 8 Print

  • Linux, centos8, nginx, reverse proxy
  • 1

Varnish Cache is a free and open-source reverse-proxy HTTP accelerator for websites and APIs. It can be installed as a reverse proxy in front of any web server running on HTTP. It sits between the web server and browser and saves web pages, cookies and other data into memory. This cache will be used to serve all future requests for exactly similar content. This will increase the web application load and improve the web server performance by 300+ times.

In this tutorial, we will set up Nginx server as a backend server and configure it to listen on port 8080, then configure Varnish cache to listen on default HTTP port 80.

Prerequisites

  • A server running CentOS 8.
  • A root password is configured the server.

Install and Configure Nginx Web Server

First, you will need to install the Nginx web server in your system. You can install it with the following command:

dnf install nginx -y

Once installed, start the Nginx service and enable it to start at system reboot with the following command:

systemctl start nginx
systemctl enable nginx

By default, the Nginx is listening on port 80. So you will need to configure Nginx to listen on port 80. You can configure it by editing Nginx default configuration file:

nano /etc/nginx/nginx.conf

Find the following lines:

        listen       80 default_server;
        listen       [::]:80 default_server;

And, replace them with the following lines:

        listen       8080 default_server;
        listen       [::]:8080 default_server;

Save and close the file when you are finished. Then, restart the Nginx service to apply the changes:

systemctl restart nginx

At this point, the Nginx web server is installed and listening on port 8080. You can check it using the following command:

netstat -tpln | grep 8080

You should get the following output:

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      30367/nginx: master 
tcp6       0      0 :::8080                 :::*                    LISTEN      30367/nginx: master 

Install Varnish Cache

By default, Varnish package is available in the CentOS 8 default repository. You can install it by just running the following command:

dnf module install varnish

Once installed, start the Varnish service with the following command:

systemctl start varnish

You can also verify the installed version of Varnish cache with the following command:

varnishd -V

You should see the following output:

varnishd (varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2018 Varnish Software AS

Configure Varnish for Nginx

Next, you will need to configure varnish to run on port 80 to receive HTTP requests from clients. You can configure it by editing Varnish default configuration file:

systemctl edit --full varnish

Find the following line:

ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m

And, replace it with the following line:

ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

Save and close the file when you are finished.

Next, you will need to define the backend server for Varnish. You can do it by editing Varnish main configuration file /etc/varnish/default.vcl:

nano /etc/varnish/default.vcl

Change the following lines:

 

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

Save and close the file when you are finished. Then, reload the systemd daemon with the following command:

systemctl daemon-reload

Next, restart the Varnish service and enable it to start at system reboot with the following command:

systemctl restart varnish
systemcl enable varnish

You can also verify the status of the Varnish with the following command:

systemctl status varnish

You should get the following output:

? varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/etc/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-08-29 09:36:58 EDT; 12s ago
  Process: 30421 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 30424 (varnishd)
    Tasks: 217
   Memory: 91.8M
   CGroup: /system.slice/varnish.service
           ??30424 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
           ??30434 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

Aug 29 09:36:57 centos8 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Aug 29 09:36:58 centos8 varnishd[30424]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Aug 29 09:36:58 centos8 varnishd[30424]: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Child (30434) Started
Aug 29 09:36:58 centos8 varnishd[30424]: Child (30434) Started
Aug 29 09:36:58 centos8 varnishd[30424]: Child (30434) said Child starts
Aug 29 09:36:58 centos8 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

You can now verify listening port of Nginx and Varnish with the following command:Advertisement

 

netstat -tpln | grep 80

You should see the following output:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30424/varnishd      
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      30367/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      30424/varnishd      
tcp6       0      0 :::8080                 :::*                    LISTEN      30367/nginx: master 

Configure SELinux and Firewall

By default, SELinux is enable in CentOS 8. So you will need to configure SELinux for Varnish. You can configure it with the following command:

setsebool -P httpd_can_network_connect 1

Next, you will need to allow port 80 through firewalld. You can do it with the following command:

firewall-cmd --permanent --zone public --add-port 80/tcp
firewall-cmd --reload

Once you are finished, you can proceed to the next step.

Test Varnish Cache

At this point, Varnish cache is installed and configured with Nginx web server. Now, its time to test whether Varnish cache is works or not.

You can test it using the curl command as shown below:

curl -I http://localhost

You should see the following output:

																																																																																																																																																																																																																																																																																																																																																																		
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sat, 29 Aug 2020 13:53:44 GMT
Content-Type: text/html; charset=UTF-8
X-Powered-By: PHP/7.2.24
X-Varnish: 32800
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

Conclusion

Congratulations! you have successfully installed Varnish Cache with Nginx on CentOS 8.


Was this answer helpful?

« Back