We have Similar setup to this diagram
Where request arrives to HAProxy, it get's roundrobin balanced to any servers, backend server checks its cache and if resource is not on that server it issues redirect with header set to the correct server IP.
Second time request arrives to HAProxy, it detects that the header with backend server is there, but how can I take that IP and direct request directly to it?
For example, second time request arrives to haproxy it has header X-BACKEND-IP=10.0.0.5
So instead haproxy trying to load balance that request, I want it to read the header, take that IP and go directly to that backend.
Is that possible? If not, would it be possible with nginx ?
Assuming you're happy with trusting the IP in the header of the second request, then yes, you can do it with use-server
:
backend bk_foo
[...]
server srv_0a_00_01_05 10.0.1.5:80 weight 100
server srv_0a_00_02_05 10.0.2.5:80 weight 100
use-server %[req.hdr(x-backend-ip),lower,map_str(/etc/haproxy/hdr2srv.map,srv_any)] if { req.hdr(x-backend-ip),lower,map_str(/etc/haproxy/hdr2srv.map) -m found }
Contents of /etc/haproxy/hdr2srv.map
:
#ip srv_name
# hex of IP used for names in this example
10.0.1.5 srv_0a_00_01_05
10.0.2.5 srv_0a_00_02_05
If you need to down one of the servers, you should dynamically update the map to remove it, so that the requests with the header set get redirected again.
If you have multiple backends, you can do similar with use_backend
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With