Absolutely every user who is faced with the development of websites and applications understands that in the modern world of Internet technologies, the performance of the project is a critical factor. Fast loading pages not only improve user experience, but also increase the SEO rating of the site. It also has a great effect on the dynamics of profit growth of your project. One of the tools that can significantly improve the performance of HTTP requests is Varnish Cache. This article aims to consider the benefits of using Varnish and how it can be useful for you. The first thing to mention is that Varnish is a high-performance HTTP accelerator that is used to cache website content. Its job is to install itself in front of the web server and process requests from clients, returning cached copies of pages instead of contacting the web server each time. This significantly reduces the load on the server and speeds up the processing of requests. That is, this tool actually speeds up, sometimes even several times, the work of your server.
How to Install and Manage Varnish
Installation methods depend on the specific OS, in this guide it is focused on Ubuntu 22.04. Varnish can be installed using the following command:
apt install varnish
After you have successfully installed Varnish, you need to configure it to work with your web server. By default, Varnish listens on port 6081. You can change the configuration in the file
/etc/varnish/default.vcl.
For example, it may look like this if you decide to change the port (for example, the http-alt port)
backend default {
.host = "127.0.0.1";
.port = "8080";
Example of working with Varnish
There are several variations for working with Varnish. For example, you need to configure the cache time to live (TTL) and you can configure it depending on the type of content. For example, for a news site, you can set a short cache time to live for current news and a longer one for archived news and not so current materials.
sub vcl_backend_response {
if (bereq.url ~ "^/news") {
set beresp.ttl = 2m;
} else {
set beresp.ttl = 72h;
}
}
How can you use it to improve HTTP performance
There are several important benefits that users can gain by using Varnish to improve their HTTP performance. Some of them are listed below:
- Anonymization. Internet users often experience the process of scanning a domain name. This may indicate a reverse proxy, it will get information about the proxy server and not the real web server behind it.
- Security. A website protected by a reverse proxy can avoid malicious attacks more easily. This will help keep the website running smoothly without causing any loss to your customers.
- SSL download or SSL termination. A reverse proxy can accept all HTTPS requests and perform an SSL handshake with the user’s browser. These requests are then converted to HTTP and sent to the web server. This frees your server from the SSL handshake and it can take care of other important activities (like downloading the rest of the web page content);
- And finally, Caching! If you allow a reverse proxy to temporarily store static content on your pages, it will be processed whenever a new request is made to it: there will be no roundtrip to the original server, and your pages will load faster.