Advance

How to install Varnish Cache 6 for Nginx on CentOS 8

How to install Varnish Cache 6 for Nginx on CentOS 8
0
(0)

You may know about Varnish. However, in this article, you will learn How to install Varnish Cache 6 for Nginx on CentOS 8. Varnish Cache is an open-source, powerful, and fast reverse-proxy HTTP accelerator with modern architecture and flexible configuration language. Being a reverse proxy simply means it is a software that you can deploy in front of your web server such as Nginx. To receive clients HTTP requests and forward to the origin server for processing. And it delivers the response from the origin server to clients.

But how does Varnish work?

Varnish acts as a middleman between Nginx and clients but with some performance benefits. Its main purpose is to make your applications load faster, by working as a caching engine. It receives requests from clients and forwards them to the backend once to cache the requested content. Then all future requests for exactly similar content will be served from the cache. This makes your web applications load faster and indirectly improves your web server’s overall performance because Varnish will serve content from memory instead of Nginx processing files from the storage disk.

Apart from caching, Varnish also has several other use cases including an HTTP request router, and load balancer, web application firewall, and more.

The varnish is configured using the highly extensible built-in Varnish Configuration Language (VCL) which enables you to write policies on how incoming requests should be handled. You can use it to build customized solutions, rules, and modules.

Recommended Article: Tutorial login and use webmail in Cpanel Host

 

How to install Varnish Cache 6 for Nginx on CentOS 8

Join us to go through this guide and learn to install the Nginx web server and Varnish Cache 6 on a fresh CentOS 8 or RHEL 8 server.

We suggest you, to set up, a complete LEMP stack instead of installing the Nginx web server alone, check out the following guides:

How to install LEMP on CentOS 8

 

And do not miss some related articles:

How to install varnish cache for Apache on CentOS/RHEL 8

1- Install Nginx Web Server on CentOS 8

The CentOS/RHEL 8 ships with the latest version of Nginx web server software, so install it from the default repository.

dnf update  dnf install nginx  

Start, enable, and verify the status. when the installation is finished.

systemctl start nginx  systemctl enable nginx  ystemctl status nginx

To receive a certain result, check the Nginx TCP socket, which runs on port 80 by default.

ss -tpln

As we asked you to run the firewall at the beginning of this guide, now make sure to update the firewall.

firewall-cmd --zone=public --permanent --add-service=http  firewall-cmd --reload

 

2- Installing Varnish Cache 6 on CentOS 8

The CentOS/RHEL 8 provides a Varnish Cache DNF module by default which contains version 6.0 LTS.

To install the module:

dnf module install varnish  

Then, you can confirm the version of Varnish installed on your system.

varnishd -V    

Please be aware that after installing Varnish Cache, the main executable command installed under /usr/sbin/varnishd and varnish configuration files are located in /etc/varnish/.

The file /etc/varnish/default.vcl is the main varnish configuration file written using VCL and /etc/varnish/secret is the varnish secret file.

Now, start the Varnish service, enable it to auto-start during system boot and confirm that it is up and running.

 

3- Configuring Nginx to Work with Varnish Cache

It is time to show you how o configure Varnish Cache to run in front of Nginx. By default Nginx listens on port 80, normally every server block (or virtual host) is configured to listen on this port.

Also, you can review the default nginx server block configured in the main configuration file (/etc/nginx/nginx.conf)

vi /etc/nginx/nginx.conf  

In case you want to run Varnish in front of Nginx, you need to change the default Nginx port from 80 to 8080 or any other port of your choice.

This should be done in all future server block configuration files (usually created under /etc/nginx/conf.d/) for sites or web applications that you want to serve via Varnish.

For example, the server block for our test site eldernode.lan is /etc/nginx/conf.d/eldernode.lan.conf and has the following configuration.

server {          listen       8080;          server_name  www.eldernode.lan;          root         /var/www/html/eldernode.lan/;          location / {          }            error_page 404 /404.html;              location = /40x.html {          }          error_page 500 502 503 504 /50x.html;              location = /50x.html {          }  }

Point: Do not forget to disable the default server block by commenting out its configuration section in the /etc/nginx/nginx.conf file, which enables you to start running other web sites/applications on your server, otherwise, Nginx will always direct requests to the default server block.

Next, check the configuration file for any errors and restart the Nginx service to apply recent changes.

nginx -t  systemctl restart nginx

You must configure Varnish to run on port 80, to be able to receive HTTP requests from clients. Unlike on earlier versions of Varnish Cache where this change was made in the Varnish environment file (which is now deprecated), in version 6.0 and above.

After that, you should make the required change in the Varnish service file. Run the following command to open the appropriate service file for editing.

systemctl edit --full  varnish

Here, you need to find the following line and change the value of the -a switch, which specifies the listen address and port. Set the port to 80.

Note: If you do not specify an address, varnishd will listen on all available IPv4 and IPv6 interfaces active on the server.

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

You can save the changes in the file and exit now.

Next, you need to define the backend server that Varnish will visit to fetch content from. You will do it in the Varnish main configuration file.

vi /etc/varnish/default.vcl

Let’s verify the default backend configuration section and change the string “default” to server1. And then set the port to 8080.

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

Since you are running Varnish and Nginx on the same server. If your Nginx web server is running on a different host. For example, another server with address 10.42.0.247, then set .host parameter.

backend server1 {      .host = "10.42.0.247";      .port = "8080";  }

You can save the changes in the file and exit now.

Next, you need to reload the systemd manager configuration because of the recent changes in the Varnish service file, then restart the Varnish service to apply the changes as follows.

systemctl daemon-reload  systemctl restart varnish

And then, confirm that Nginx and Varnish are listening on the configured TCP sockets.

ss -tpln

 

buy VPS with bitcoin

 

4- Testing Nginx Varnish Cache Setup

In this step, you will verify the web pages are being served via Varnish Cache as follows. Open a web browser and navigate using the server IP or FDQN.

Use your server’s IP address or web site’s FQDN or use 127.0.0.1 or localhost if you are testing locally.

curl -I http:///www.tecmint.lan

 

Useful Varnish Cache Administration Utilities

Follow this section to let us briefly describe some of the useful utility programs that Varnish Cache ships with, that you can use to control varnishd, access in-memory logs and overall statistics, and more.

varnishadm

varnishadm a utility to control a running Varnish instance. It establishes a CLI connection to varnishd. For example, you can use it to list configured backends as shown in the following screenshot.

Point: Read man varnishadm for more information.

varnishadm  varnish> backend.list

varnishlog

The varnishlog utility provides access to request-specific data. It offers information about specific clients and requests.

Point: Read man varnishlog for more information.

varnishlog    

varnishstat

varnishstat or varnish statistics. Which gives you a glance at Varnish’s current performance by providing access to in-memory statistics such as cache hits and misses, information about the storage, threads created, deleted objects.

Point: Read man varnishstat for more information

varnishstat

 

varnishtop

varnishtop utility reads the shared memory logs and presents a continuously updated list of the most commonly occurring log entries.

Point: Read man varnishtop for more information.

varnishtop   

 

varnishhist

varnishhist (varnish history) utility parses the varnish logs and outputs a continuously updated histogram showing the distribution of the last n requests by their processing.

Point: Read man varnishhist for more information.

varnishhist  
Recommended Article: How to upgrade php version on Linux server and DirectAdmin

Good job! By touching this point, you have finished the guide and learned how to install Varnish Cache and run it in front of the Nginx HTTP server to accelerate web content delivery in CentOS/RHEL 8.

It is useful to know that the main drawback of Varnish Cache is its lack of native support for HTTPS. To enable HTTPS on your web site/application, you need to configure an SSL/TLS termination proxy to work in conjunction with Varnish Cache to protect your site.

 

Dear user, we wish this tutorial would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

View More Posts
Marilyn Bisson
Content Writer
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day