Varnish Cache technology increases performance by keeping duplicate web pages in memory. In effect, when a user searches for a web page, it receives a cached copy, bypassing the time-consuming process of waiting for the original web server to recreate the page. This function provides better control over the performance of your website and allows for more fine-tuning for the main results. Because Varnish Cache is open source and user-friendly, it is used by millions of websites worldwide to increase performance. In this post, we will tell you how you can Experience Lightning-Fast Website Loading with Varnish Cache on AlmaLinux.
Varnish Cache is an open-source web application accelerator that helps optimize web pages for faster loading. It does this by storing copies of web pages in memory. When a user requests a web page, it retrieves the cached version instead of waiting for the original web server to generate the page from scratch.
This reduces server load and page load times, making websites more responsive and improving user experience. Varnish also allows you to control how pages are stored in your cache using HTTP cache control headers. Using these, you can specify when the cached version of a page should expire before Varnish sends it back to the origin server to be regenerated.
This gives you more control over the performance of your website and allows you to fine-tune it even more for optimal results. Because it’s open-source and relatively easy to use, millions of websites around the web now use Varnish Cache to improve performance.
Varnish Cache on AlmaLinux offers several significant benefits that enhance the performance and user experience of a website:
1- Faster Content Delivery: Varnish Cache stores a copy of the most commonly accessed pages on your website in memory. This reduces the need for frequent requests to your server, resulting in significantly faster delivery of content to end users.
2- Reducing Server Load: Because Varnish Cache serves content from its own cache instead of relying on the server to regenerate content for each request, it significantly reduces server load and increases the overall performance of your website.
3- Scalability: Cache Varnish can help your website handle increased traffic more easily by serving cached content to a large number of concurrent users. This feature makes it a great tool for scalability.
4- Ability to Customize: Varnish Cache uses a flexible programming language called VCL. This allows you to create specific storage rules and policies tailored to your website’s needs.
5- Increasing Accessibility: In cases where the backend server is down or unreachable, Varnish Cache can serve the old version of the content from its cache. As a result, the availability and uptime of your site will increase.
6- Edge Side Includes (ESI) support: Cache Varnish supports ESI. A technology that allows you to cache different parts of a web page separately. This feature is especially useful for websites with dynamic content.
7- GeoIP support: With Varnish, you can serve localized content using GeoIP extensions to identify users’ geographic locations.
These benefits make Varnish Cache an invaluable tool on AlmaLinux for anyone looking to increase the performance, scalability, and reliability of their web server.
Before we start teaching how to install Varnish Cache on AlmaLinux, it is necessary to have a Linux VPS server with the AlmaLinux operating system.
In the first step, you must log in to the server using the following command through SSH as the root user:
ssh root@IP_ADDRESS -p PORT_NUMBER
Update the packages on the server with the help of the following command:
dnf update -y
Disable the default Varnish repo by running the following command:
dnf module disable varnish
Now you need to install the EPEL repository:
dnf install epel-release -y
Then you can install the Varnish repo using the following command:
curl -s https://packagecloud.io/install/repositories/varnishcache/varnish70/script.rpm.sh | bash -
Finally, you can install Varnish on Almalinux using the following command:
dnf install varnish -y
After the successful installation of Varnish, you should now verify the version of Varnish by running the following command:
rpm -qi varnish
You can start and enable Varnish using the following commands and view the installation status:
sudo systemctl start varnish sudo systemctl enable varnish sudo systemctl status varnish
In this section, we will teach how to configure the varnish cache on AlmaLinux. In order for Varnish to listen on port 80, you need to open the configuration file using a text editor:
nano /usr/lib/systemd/system/varnish.service
Now you can change the default port 6081 to port 80 using the following command:
ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,2g
After saving the configuration file and exiting it, you can now reload the systemd daemon by running the following command:
sudo systemctl daemon-reload
Finally, to apply the changes, restart Varnish with the help of the following command:
sudo systemctl restart varnish
To configure Nginx to work with Varnish, you need to first install the Nginx package:
sudo dnf install nginx
Then you need to run the Nginx configuration file using a text editor:
nano /etc/nginx/nginx.conf
Change the listening port to 8080 as follows:
..... server { listen 8080 default_server; listen [::]:8080 default_server; server_name _; root /usr/share/nginx/html; .....
After saving the configuration file, restart Nginx to apply the changes:
sudo systemctl restart nginx
In the final step, it is necessary to open access to the HTTP service in the firewall:
sudo firewall-cmd --zone=public --permanent --add-service=http
Also, reload the firewall settings to apply the new changes:
sudo firewall-cmd --reload
In this section, we are going to check the performance of cache varnish using wrk. Note that wrk is a modern tool written in C language and used to measure HTTP. This tool can be used to load test web servers with many requests per second. To install wrk, it is necessary to first install some build tools for C and git using the following command:
sudo apt-get install build-essential libssl-dev git unzip -y
In the next step, you can clone the git repository for wrk in the wrk directory by running the following command:
git clone https://github.com/wg/wrk.git work
Now you can easily change to that new directory:
cd wrk
After changing to the new directory, it’s time to build the wrk executable with the make command:
make
Copy wrk to the corresponding folder as in the command below. By doing this you will be able to access it from anywhere in your directory structure:
sudo cp wrk /usr/local/bin
You can use wrk to test Apache responsiveness:
wrk -t2 -c1000 -d30s --latency http://server_ip/
The meaning of the parameters in the above command is as follows:
The output of the above command will be as follows:
output Running 30s test @ http://your_ip_address/ 2 threads and 1000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 44.45ms 104.50ms 1.74s 91.20% Req/Sec 8.29k 1.07k 12.40k 71.00% Latency Distribution 50% 11.59ms 75% 22.73ms 90% 116.16ms 99% 494.90ms 494677 requests in 30.04s, 5.15GB read Socket errors: connect 0, read 8369, write 0, timeout 69 Requests/sec: 16465.85 Transfer/sec: 175.45MB
Now it’s time to run the same test for the Varnish server by running the following command:
wrk -t2 -c1000 -d30s --latency http://server_ip:8080/
The output of the above command will be as follows:
output Running 30s test @ http://server_ip:8080/ 2 threads and 1000 connections Thread Stats Avg Stdev Max +/- Stdev Latency 14.41ms 13.70ms 602.49ms 90.05% Req/Sec 6.67k 401.10 8.74k 83.33% Latency Distribution 50% 13.03ms 75% 17.69ms 90% 24.72ms 99% 58.22ms 398346 requests in 30.06s, 4.18GB read Socket errors: connect 0, read 19, write 0, timeout 0 Requests/sec: 13253.60 Transfer/sec: 142.48MB
In some cases, the varnish may show incorrect behavior. In other words, it doesn’t behave the way you want it to. There are a few places you can check to troubleshoot these, including:
In the following, we will introduce you to the basic troubleshooting method in Varnish.
1) Varnish won’t Start
Sometimes the varnish may not start. There are many reasons for not starting Varnish. Start Varnish in debug mode with the following command:
varnishd -f /usr/local/etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1: 2000 -a 0.0.0.0:8080 -d
The output of the above command will be as follows:
Using old SHMFILE Platform: Linux,2.6.32-21-generic,i686,-smalloc,-hcritbit 200 193 ----------------------------- Varnish Cache CLI. ----------------------------- Type 'help' for command list. Type 'quit' to close CLI session. Type 'start' to launch worker process.
Now you can tell the main process to start the cache by running the command:
start
bind(): Address already in use
300 22
Could not open sockets
2) Varnish is Crashing (panics)
The next thing is that when the varnish wears off, the child’s processing may be damaged. Note that when Varnish encounters this, the save process will be disabled in a controlled manner. It should be noted that this failure may be due to incorrect configuration. You can check the status of panic messages by running the following command:
panic.show
The output of the above command may be as follows:
Assert error in ESI_Deliver(), cache_esi_deliver.c line 354: Condition(i == Z_OK || i == Z_STREAM_END) not true. thread = (cache-worker) ident = Linux,2.6.32-28-generic,x86_64,-sfile,-smalloc,-hcritbit,epoll Backtrace: 0x42cbe8: pan_ic+b8 0x41f778: ESI_Deliver+438 0x42f838: RES_WriteObj+248 0x416a70: cnt_deliver+230 0x4178fd: CNT_Session+31d (..)
3) Varnish is Crashing (segfaults)
The next error you may encounter is Varnish crashing (segfaults). In other words, Varnish may encounter a segmentation fault. When this event is registered by the child process, the core is unloaded and the child process is restarted. But to debug a segfault, you need to provide some data.
First, you need to make sure you have installed Varnish with debug symbols. After that, you need to make sure that kernel dump is allowed in the main shell:
ulimit -c unlimited
Open the kernel with gdb and issue the following command. By doing this you will get a stack trace of the thread that caused the segfault error:
bt
4) Varnish gives me Guru Meditation
To fix this problem, it is necessary to first find the corresponding log entries in varnishlog. Since it can be difficult to trace the entries, you can set varnishlog to log all your 503 errors using the following command:
$ varnishlog -q 'RespStatus == 503' -g request
To get varnishlog to process the entire shared memory log, just run the following command:
$ varnishlog -d -q 'RespStatus == 503' -g request
To get the most out of Varnish Cache in AlmaLinux, it’s important to follow best practices. Some key best practices include:
1) Fine-tune the Varnish configuration: Experiment with different TTL values and URL patterns to find the optimal configuration for your website.
2) Monitor website performance: Regularly monitor website performance using tools like GTmetrix or Pingdom.
3) Keep Varnish Cache up-to-date: Update Varnish Cache regularly to make sure you’re using the latest version with the latest features and bug fixes.
10 alternatives to Varnish Cache are:
1) ApacheBooster
2) Squid-Cache
3) Speed Kit
4) WampServer
5) W3 Total Cache
6) Amazon DynamoDB Accelerator (DAX)
7) TwicPics
8) F5 NGINX
9) F5 NGINX Plus
10) Varnish Software
As you read in this article, Varnish Cache is a powerful open-source web application accelerator that is widely used to increase the speed and performance of websites. By storing cached versions of web pages, it significantly reduces server load and improves page load times. Customization through its configuration language allows tailored storage rules based on specific website needs. Due to the use of Varnish cache and its importance, in this article, we tried to teach you how to Lightning-Fast Website Loading with Varnish Cache on AlmaLinux.
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.
In this article, we are going to teach you How to Install OpenCart on AlmaLinux 8. The OpenCart Stor...
If you are going to prevent malicious traffic or data coming from the Internet or other networks to ...
What is your opinion about this Blog?