我需要在响应 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
答案1
为了实现这一点,请将该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
然而,更好的扩展替代方案是在节cookie
中使用backend
:
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
希望这可以帮助!
答案2
我们应该使用srv_id
为其定义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