I just started using HAProxy and I have the following situation:

  1. I have two backend services, lets call them service A and service P
  2. I am trying to use a load balancer like haproxy
  3. When a request comes in, assume that it has a token that is part of a HTTP header like X-SOME-TOKEN that is httponly
  4. I want to extract this token, send to it service A, retrieve an ID token, attach it to a request for service B which, if returns HTTP status code 200, only then a success HTTP status code 200 and the JSON payload sent from service B is sent to client and if otherwise, HTTP status code 500 is sent to client.

My confusion is with part 3 and 4. I am not sure if this can be done at the load balancing layer using haproxy. I have looked at the docs and read briefly about acl. If this can be done using acl what should be the possible approach?

To add more context, this is the configuration file I have with comments on where I would like to make changes:

  log /dev/log daemon
  maxconn 32768
  chroot /var/lib/haproxy
  user haproxy
  group haproxy
  stats socket /var/lib/haproxy/stats user haproxy group haproxy mode 0640 level admin

  tune.bufsize 32768
  tune.ssl.default-dh-param 2048
  ssl-default-bind-ciphers ALL:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK:!RC4:!ADH:!LOW@STRENGTH

   log     global
   mode    http
   option  log-health-checks
  option  log-separate-errors
  option  dontlog-normal
  option  dontlognull
  option  httplog
  option  socket-stats
  retries 3
  option  redispatch
  maxconn 10000
  timeout connect     5s
  timeout client     50s
  timeout server    450s

#listen stats
  #bind :::80 v6only
  #stats enable
  #stats uri     /
  #stats refresh 5s
  #rspadd Server:\ haproxy/1.6

frontend testnode
  # I want to define all my urls here for which,
  # the load balancer needs to listen to on port 4200
  mode http
  default_backend testnodebackend

backend testnodebackend
   # this is where I believe I need to add the proper functions
   # based on url endpoint like mydomain.com/endpoint, I want 
   # to decide where service A needs to be called for token exchange
   # this configuration currently points to two instances of the same 
   # server
   balance roundrobin
   option forwardfor
   http-request set-header X-Forwarded-Port %[dst_port]
   option httpchk HEAD / HTTP/1.1\r\nHost:localhost
   server node1 check 
   server node2 check


I have found that custom request handlers can be added in HAProxy using Lua as a scripting language. I have already set up my configuration which, can run "hello_world" examples from HAProxy. However, when it tries to load modules in Lua (like socket module), there is an import error. As soon as I have that fixed, I will add more on how setup these custom request handlers.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.