5

I need to add server name in response HTTP headers X-Servedby. Is there any way to replace [server] with name of server that has served request?

frontend front x.x.x.x:80
  default_backend balancing
  rspadd X-Servedby:\ [server] #I need to replace [server]

backend balancing
  server srv1 x.x.x.x:80 check
  server srv2 x.x.x.x:80 check
Belmin Fernandez
  • 10,629
  • 26
  • 84
  • 145
Kirzilla
  • 543
  • 3
  • 8
  • 20

2 Answers2

7

To achieve this, change the frontend stanza to the following:

frontend front x.x.x.x:80
    default_backend balancing
    acl srv1 srv_id 1
    acl srv2 srv_id 2
    rspadd X-Servedby:\ srv1 if srv1
    rspadd X-Servedby:\ srv2 if srv2

However, a better scaling alternative would be to use cookie in the backend stanza:

backend balancing
    cookie SRVNAME insert
    server srv1 x.x.x.x:80 cookie srv1 check
    server srv2 x.x.x.x:80 cookie srv2 check

Hope this helps!

Belmin Fernandez
  • 10,629
  • 26
  • 84
  • 145
  • `rspadd X-Servedby:\ srv1 if srv1` is causing `error detected while parsing a 'rspadd' condition`. I suppose that srv1 in this condition should be defined as ACL, but it seems impossible to define ACL depending on server name. About cookies - this method will cause all requests that have cookie SRVNAME=srv1 to be served by srv1 server isn't it? – Kirzilla Jul 03 '14 at 15:52
  • I've just found `srv_id` that could be used in ACL. – Kirzilla Jul 03 '14 at 15:55
  • Sorry, not at my desk. Hope it gives you an idea at least. I will double check my config when I get to my desk. – Belmin Fernandez Jul 03 '14 at 16:03
  • Great! Please add defining `id 1`, `id 2` to `server` directive. – Kirzilla Jul 03 '14 at 16:03
  • @Belin, thank you! I've posted more complex config for someone solving the same problem. – Kirzilla Jul 03 '14 at 16:06
2

We should use srv_id which provides id value defined for server

frontend front x.x.x.x:80

  acl serve_us1 url_beg /west
  acl serve_us2 url_beg /east

  #defining acl for srv_id
  acl served_by_us1 srv_id 1  #look at srv_id
  acl served_by_us2 srv_id 2  #look at srv_id

  use_backend us1 if serve_us1
  use_backend us2 if serve_us2 
  default_backend balancing

  rspadd X-ServedBy:\ us1 if served_by_us1
  rspadd X-ServedBy:\ us2 if served_by_us2

backend us1
  server srv1 1.1.1.1:80 check id 1 #look at id

backend us2
  server srv2 2.2.2.2:80 check id 2 #look at id

backend balancing
  server srv1 1.1.1.1:80 check id 1 #look at id
  server srv2 2.2.2.2:80 check id 2 #look at id
Kirzilla
  • 543
  • 3
  • 8
  • 20