HAProxy server addresses

We have a similar setup to this diagram

enter image description here

When a request arrives at HAProxy, it gets a rounded balance on any servers, the backend server checks its cache, and if the resource is not on that server, it issues a redirect with a header set to the correct server IP address.

The second time request goes to HAProxy, it detects that there is a header with the backend server, but how can I pass this IP address and direct the request directly to it?

For example, the second time request goes to haproxy, it has a header X-BACKEND-IP=10.0.0.5

So, instead of haproxy trying to load the balance of this request, I want it to read the header, accept this IP, and go directly to that server.

Is it possible? If not, is this possible with nginx?

+3


source to share


1 answer


Assuming you are happy with trusting the IP in the second request header, 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 /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 go down to one of the servers, you must dynamically update the map to remove it so that headers set requests are redirected again.

If you have multiple backends, you can do the same with use_backend

.

+2


source







All Articles