Search code examples
haproxy

Setting Custom Header on a per server basis in HAProxy


I need to load balance 3rd party services using HAProxy 1.7. Each of the servers requires unique Basic Auth Headers. I am looking for an approach similar to this below, where I can "roundrobin" between backend servers, but each server needs a different HTTP header:

frontend http-in
    bind *:80
    use_backend servs

backend servs
    reqidel '^Authorization:.*'
    reqadd 'Authorization: Basic blahblahblah'
    server url1 asdf.example.com:8080 check ssl verify none
    reqidel '^Authorization:.*'
    reqadd 'Authorization: Basic blah2blah2blah2'
    server url2 asdf.example.com:8081 check ssl verify none

This approach only ever uses the first server (url1).


Solution

  • I implemented the following solution to allow for custom headers for each server being load balanced.

    frontend http-in
        bind *:80
        use_backend proxy
    
    backend proxy
        balance roundrobin
        server url1-proxy 0.0.0.0:8080
        server url2-proxy 0.0.0.0:8081
    
    listen url1-proxy
        bind *:8080
        reqidel '^Authorization:.*'
        reqadd 'Authorization: Basic blahblahblah'
        server url1 asdf.example.com:8080 check ssl verify none
    
    listen url2-proxy
        bind *:8081
        reqidel '^Authorization:.*'
        reqadd 'Authorization: Basic blah2blah2blah2'
        server url2 asdf.example.com:8081 check ssl verify none