Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 5 down vote accepted

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!

share|improve this answer
    
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 3 at 15:52
    
I've just found srv_id that could be used in ACL. –  Kirzilla Jul 3 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 3 at 16:03
    
Great! Please add defining id 1, id 2 to server directive. –  Kirzilla Jul 3 at 16:03
    
@Belin, thank you! I've posted more complex config for someone solving the same problem. –  Kirzilla Jul 3 at 16:06

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.