Load Balancer Varnish
Introduction
Varnish is a high-performance HTTP reverse proxy, primarily designed to act as a web application accelerator through in-memory caching.
It is positioned between clients and backend servers, storing HTTP responses in cache to reduce the load on application servers and improve request response times.
Although it does not natively support TLS, it is typically used behind an SSL terminator, such as NGINX or HAProxy, allowing Varnish to operate internally with HTTP while external clients use HTTPS.
It is ideal when:
- the site has high traffic volume
- many requests are repeated
- the backend is expensive or slow to generate responses
- you want to reduce load on application servers
Besides caching, Varnish also offers routing and request distribution features among multiple backend servers, allowing it to function as a basic HTTP load balancer.
Configuring Varnish as a Balancer
Step 1 – Setting up the Topology
Add Varnish with a Public IPv4 in the balancing layer. After that, apply the change.

Step 2 – Domain Pointing
- Point your domain to the Public IP of Varnish.
- Access the directory
/etc/varnish, the configuration file is default.vcl

- The default.vcl file comes preconfigured when creating the topology, below is an example of the file with 2 backend servers: Note: IP_INTERNO will display the internal IP of your application.
vcl 4.0;
import std;
import directors;
backend serv2 { .host = "IP_INTERNO "; .port = "80"; .probe = { .url = "/"; .timeout = 30s; .interval = 60s; .window = 5; .threshold = 2; } }
backend serv1 { .host = "IP_INTERNO "; .port = "80"; .probe = { .url = "/"; .timeout = 30s; .interval = 60s; .window = 5; .threshold = 2; } }
sub vcl_init {
new myclust = directors.hash();
myclust.add_backend(serv2, 1);
myclust.add_backend(serv1, 1);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_recv {
if (req.http.Upgrade ~ "(?i)websocket") {
set req.backend_hint = myclust.backend(client.identity);
return (pipe);
}
else {
set req.backend_hint = myclust.backend(client.identity);
}
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
sub vcl_fini {
return (ok);
}
After adjustments if necessary, just restart the Varnish node and if everything is correct it will show in the upper right corner that it was successfully restarted:
