Load-Balancing

如何在前端部分獲取已服務請求的伺服器的名稱?

  • March 1, 2022

我需要添加伺服器名稱以響應 HTTP 標頭 X-Servedby。有什麼辦法可以代替

$$ server $$帶有已服務請求的伺服器的名稱?

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

為此,請將frontend節更改為以下內容:

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

但是,更好的縮放替代方法是cookiebackend節中使用:

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

希望這可以幫助!

我們應該使用srv_idwhich 提供id定義的值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

引用自:https://serverfault.com/questions/609915