'Can we perform mapping or do a lookup of string or KV pair in Varnish `sub vcl_recv`
I am trying to route the traffic/connections to backend director based on url parameters which are passed on incoming request.
Here is my current config
sub vcl_recv {
if (req.url ~ "sto") {
set req.backend_hint = endpoint1.backend();
}
}
Currently, above config does static mapping/check for sto. I would like to do a dynamic lookup from map/hash/KV file and do condition check with url contents.
Here is how I do it in haproxy
use_backend %[path,map_sub(/etc/haproxy/GET.map)]
#>cat GET.map
# url paramter backend location/name
/sgtier get_backend1
/sto get_backend2
Config file
vcl 4.0;
import directors;
backend sg_gw1 {
.host = "1.8.4.4";
.port = "1043";
}
backend tss_gw1 {
.host = "1.8.13.2";
.port = "8080";
}
sub vcl_recv {
if (req.url ~ "sto") {
set req.backend_hint = sg_gw1;
}
elsif (req.url ~ "common") {
set req.backend_hint = tss_gw1;
}
}
Solution 1:[1]
Dynamic backends are supported in Varnish Enterprise. See https://docs.varnish-software.com/varnish-cache-plus/vmods/goto/ for more information.
An open source alternative for dynamic backends can be found on https://code.uplex.de/uplex-varnish/libvmod-backend_dyn
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Thijs Feryn |
