Introduction
In the world of web performance optimization, Varnish Cache stands as a robust solution for accelerating websites and enhancing user experiences. When combined with the efficiency of Nginx, you have a winning duo that can significantly boost your web application’s speed. This guide will provide you with a step-by-step walkthrough on how to install Varnish Cache for Nginx on Rocky Linux 9, optimizing your website’s performance.
Table of Contents
Key Aspects and Features of Varnish
Here are some key aspects and features of Varnish:
- Caching: Varnish acts as an intermediary between clients and backend servers, caching the content of web pages or API responses in memory. This allows subsequent requests for the same content to be served directly from the cache, reducing the load on backend servers and improving response times.
- Reverse Proxy: Varnish operates as a reverse proxy server, receiving client requests and forwarding them to the appropriate backend servers. It can distribute requests across multiple backend servers, providing load balancing and fault tolerance.
- HTTP Acceleration: Varnish is specifically designed to accelerate web applications by optimizing and accelerating HTTP communication. It uses advanced techniques such as memory-based caching, parallel processing, and efficient data handling to deliver content quickly.
- Dynamic Content Caching: Varnish can intelligently cache dynamic content by employing caching rules based on HTTP headers or cookies. This enables efficient caching even for dynamic web applications, reducing the load on backend servers and improving overall performance.
- Configuration Language: Varnish has its own domain-specific language called VCL (Varnish Configuration Language) that allows users to define caching rules, manipulate HTTP headers, and customize the behavior of the caching proxy.
- High Scalability: Varnish is known for its ability to handle high traffic loads and scale horizontally across multiple servers or clusters. It can effectively handle thousands of concurrent connections and deliver content at high speeds.
Prerequisites
Before you dive how to install varnish Cache on Rocky Linux 9, ensure that your system meets the prerequisites for a successful setup:
- A Rocky Linux 9 system with root or sudo access, to set this up, follow our guide Initial Setup Rocky Linux 9 Server: Secure and Efficient You can deploy this on a physical machine or a virtual environment like VMware or VirtualBox.
- A stable internet connection to download packages
- Basic familiarity with command-line operations.
Install Varnish on Rocky Linux 9
In this tutorial, I will show you how to install Varnish cache with Nginx on Rocky Linux 9.
Step 1: System Update
Before starting to install Varnish on Rocky Linux 9, it’s good practice to ensure your system’s package repositories and installed packages are up to date. Use the following command to update the system:
[samm@varnish ~]$ sudo dnf update -y
[samm@varnish ~]$ sudo dnf upgrade -y
Step 2: Install Apache/ Nginx
You can install Nginx server by using our previous guides below.
- How To Build NGINX from Source (Compile) on Rocky Linux 9
- How To Build NGINX from Source (Compile) on Centos 7
Step 3: Install Varnish Cache
With Nginx in place, it’s time to install Varnish Cache. However, Varnish is not available in the default Rocky Linux repositories. To obtain it, add the EPEL repository and install Varnish:
[samm@varnish ~]$ sudo dnf install epel-release -y
[samm@varnish ~]$ sudo dnf install varnish -y
After installing Varnish, you can get the above information by running the following command in terminal.
[samm@varnish ~]$ rpm -qi varnish
You should see the following output:
Name : varnish
Version : 6.6.2
Release : 3.el9
Architecture: x86_64
Install Date: Fri 21 Jul 2023 02:09:52 AM EDT
Group : Unspecified
Size : 3831088
License : BSD
Signature : RSA/SHA256, Fri 21 Apr 2023 06:49:06 AM EDT, Key ID 702d426d350d275d
Source RPM : varnish-6.6.2-3.el9.src.rpm
Build Date : Fri 21 Apr 2023 06:42:21 AM EDT
Build Host : pb-a7e3c7d0-599f-49ec-84df-040fa9dff709-b-x86-64
Packager : Rocky Linux Build System (Peridot) <[email protected]>
Vendor : Rocky Enterprise Software Foundation
URL : https://www.varnish-cache.org/
Summary : High-performance HTTP accelerator
Description :
This is Varnish Cache, a high-performance HTTP accelerator.
Varnish Cache stores web pages in memory so web servers don’t have to
create the same web page over and over again. Varnish Cache serves
pages much faster than any application server; giving the website a
significant speed up.
Documentation wiki and additional information about Varnish Cache is
available on: https://www.varnish-cache.org/
Verify installation by running the following commands
[samm@varnish ~]$ varnishd -V
varnishd (varnish-6.6.2 revision 17c51b08e037fc8533fb3687a042a867235fc72f)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2020 Varnish Software
[samm@varnish ~]$ which varnishd
/usr/sbin/varnishd
Step 4: Manage Varnish Service
Start the Varnish service and enable it to start after system reboot with the following command:
[samm@varnish ~]$ sudo systemctl start varnish
[samm@varnish ~]$ sudo systemctl enable varnish
Created symlink /etc/systemd/system/multi-user.target.wants/varnish.service → /usr/lib/systemd/system/varnish.service
[samm@varnish ~]$ sudo systemctl status varnish
● varnish.service - Varnish Cache, a high-performance HTTP accelerator
Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; preset: disabled)
Active: active (running) since Fri 2023-07-21 02:27:36 EDT; 1s ago
Process: 123494 ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
Main PID: 123495 (varnishd)
Tasks: 217
Memory: 96.0M
CPU: 772ms
CGroup: /system.slice/varnish.service
├─123495 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
└─123516 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
Jul 21 02:27:36 varnish varnishd[123495]: Warnings:
Jul 21 02:27:36 varnish varnishd[123495]: VCL compiled.
Jul 21 02:27:36 varnish varnishd[123495]: Debug: Version: varnish-6.6.2 revision 17c51b08e037fc8533fb3687a042a867235fc72f
Jul 21 02:27:36 varnish varnishd[123495]: Version: varnish-6.6.2 revision 17c51b08e037fc8533fb3687a042a867235fc72f
Jul 21 02:27:36 varnish varnishd[123495]: Debug: Platform: Linux,5.14.0-284.11.1.el9_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Jul 21 02:27:36 varnish varnishd[123495]: Platform: Linux,5.14.0-284.11.1.el9_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Jul 21 02:27:36 varnish varnishd[123495]: Debug: Child (123516) Started
Jul 21 02:27:36 varnish varnishd[123495]: Child (123516) Started
Jul 21 02:27:36 varnish varnishd[123495]: Child (123516) said Child starts
Jul 21 02:27:36 varnish systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
Step 5: Configuring Nginx to Work with Varnish Cache
To ensure smooth interaction between Nginx and Varnish, adjustments are needed in Nginx’s configuration. Open the Nginx configuration file using a text editor, which is typically located at /etc/nginx/nginx.conf
.
Locate the server block that corresponds to your website or application and modify the configuration to listen on a different port. Change the listen
directive to something like:
[samm@varnish ~]$ sudo vi /etc/nginx/nginx.conf
server {
listen 8080;
listen [::]:8080;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
If you have configured separate virtual hosts for your sammlinux (e.g www.sammlinux.com), such as /etc/nginx/conf.d/sammlinux.conf then open its configuration with the following command
[samm@varnish ~]$ sudo vi /etc/nginx/conf.d/sammlinux.conf
### HTTP ###
server {
listen 8080;
listen [::]:8080;
server_name www.sammlinux.com;
server_tokens off;
..........
Once the configuration complete, check the configuration file for any errors and restart the Nginx service to apply recent changes.
[samm@varnish ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[samm@varnish ~]$ sudo systemctl restart nginx
Verify nginx is running on port 8080
[samm@varnish ~]$ sudo netstat -plant | grep -w 8080
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 125987/nginx: maste
tcp6 0 0 :::8080 :::* LISTEN 125987/nginx: maste
Step 6: Configure Varnish
Next, configure Varnish to listen on the default HTTP port (port 80). By default, varnish listens on port 6081. Here, we will use varnish to accept incoming HTTP connections. So you will need to configure varnish to listen on port 80. You can do it by editing /usr/lib/systemd/system/varnish.service
[samm@varnish ~]$ sudo vi /usr/lib/systemd/system/varnish.service
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
Save and close the file then reload the systemd daemon to apply the changes:
[samm@varnish ~]$ sudo systemctl daemon-reload
[samm@varnish ~]$ sudo systemctl restart varnish
Verify varnish running on port 80
[samm@varnish ~]$ sudo netstat -plant | grep -w 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 126303/varnishd
tcp6 0 0 :::80 :::* LISTEN 126303/varnishd
[samm@varnish ~]$ sudo ss -antpl | grep varnish
LISTEN 0 1024 0.0.0.0:80 0.0.0.0:* users:(("cache-main",pid=126324,fd=3),("varnishd",pid=126303,fd=3))
LISTEN 0 10 127.0.0.1:43821 0.0.0.0:* users:(("varnishd",pid=126303,fd=9))
LISTEN 0 10 [::1]:33833 [::]:* users:(("varnishd",pid=126303,fd=8))
LISTEN 0 1024 [::]:80 [::]:* users:(("cache-main",pid=126324,fd=5),("varnishd",pid=126303,fd=5))
Step 7: Configure Firewalld
Follow the below command to configure firewalld on CentOS/ Rocky Linux
[samm@varnish ~]$ sudo firewall-cmd --state
running
[samm@varnish ~]$ sudo firewall-cmd --get-default-zone
public
Allow port HTTP (80) and HTTPS (443) port in Firewall
[samm@varnish ~]$ sudo firewall-cmd --add-port=80/tcp --zone=public --permanent
success
[samm@varnish ~]$ sudo firewall-cmd --add-port=443/tcp --zone=public --permanent
success
[samm@varnish ~]$ sudo firewall-cmd --reload
success
Step 8: Verify Varnish Cache
Now that Varnish is installed and configured alongside the Nginx web server, it’s time to put the Varnish cache to the test.
Run the following command to verify the varnish cache:
[samm@varnish ~]$ curl -I localhost
If everything is fine, you will get the following output:
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 21 Jul 2023 07:11:33 GMT
Content-Type: text/html
Content-Length: 162
Location: https://sammlinux.com/
X-Varnish: 32770 3
Age: 11
Via: 1.1 varnish (Varnish/6.6)
Connection: keep-alive
Check logs with the below command:
[samm@varnish ~]$ sudo varnishncsa
Access the Web server with http://server-ip and check on the terminal with the above command you will get some logs.
If you didn’t get any logs then reload your http://server-ip webserver on the browser and check the terminal you will notice some logs being generated.
172.32.1.254 - - [21/Jul/2023:03:14:52 -0400] "GET http://172.32.1.222/ HTTP/1.1" 301 162 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
172.32.1.254 - - [21/Jul/2023:03:15:24 -0400] "GET http://172.32.1.222/ HTTP/1.1" 301 162 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
Conclusion
This tutorial has provided a comprehensive guide on install and configure Varnish Cache for Nginx on Rocky Linux 9. The strategic utilization of Varnish Cache in conjunction with Nginx offers substantial enhancements in both performance and scalability for your web applications.
Varnish Cache operates by intelligently storing frequently requested content in memory, thereby enabling direct retrieval from this cache rather than repeatedly sourcing the same content from the origin server. Nginx, on the other hand, serves as a robust web server, fulfilling roles such as a reverse proxy and a load balancer.
Also Read Our Other Guides :
- How To Build NGINX from Source (Compile) on Rocky Linux 9
- How To Install Apache on Rocky Linux 9
- How To Install Nginx on Rocky Linux 9: A Comprehensive Guide
By effectively configuring Nginx to seamlessly integrate with Varnish Cache, you’re poised to elevate the efficiency of your web server. This not only augments performance but also lightens the burden on your origin server, contributing to a smoother and more responsive user experience. Hopefully, now you have learned how to Install Varnish Cache for Nginx on Rocky Linux 9.