大网数据广告活动
服务器运维问答

HAProxy override backend cookie using query parameter

· 52CE 我爱测

Based on this answer , I can successfully override the backend with a query parameter: backend servers balance roundrobin cookie SERVERID insert indirect nocache use-server web1 if { urlp(SERVERID) -i web1 } use-server web2 if { …

问题原文

原帖链接 · Kevin · CC BY-SA 3.0

Based on this answer, I can successfully override the backend with a query parameter:

backend servers
  balance roundrobin
  cookie SERVERID insert indirect nocache
  use-server web1 if { urlp(SERVERID) -i web1 }
  use-server web2 if { urlp(SERVERID) -i web2 }
  server web1 web1-internal:80 check cookie web1
  server web2 web2-internal:80 check cookie web2

However, I would like for this to "persist" in subsequent requests; i.e. I want a new Set-Cookie with the value from the query parameter to overwrite any existing SERVERID cookies. HAProxy doesn't add a Set-Cookie if the request came in with a valid SERVERID Cookie. Removing the indirect option didn't work.

Is the only solution to use preserve and have the backend always respond with Set-Cookie, or is there a way to do this in HAProxy?

已采纳解答

原帖链接 · Josh Vickery · CC BY-SA 3.0

I did this by creating new backends that set the SERVERID cookie and routing to them via frontend directives.

frontend http_in
  acl set_current_site_cookie urlp(current_site) true
  acl set_snapshot_site_cookie urlp(snapshot_site) true

  bind *:80
  use_backend set_current_site_cookie if set_current_site_cookie
  use_backend set_snapshot_site_cookie if set_snapshot_site_cookie
  default_backend servers

backend set_current_site_cookie
  cookie SERVERID insert
  server current_site server.com:80 cookie current_site

backend set_snapshot_site_cookie
  cookie SERVERID insert
  server snapshot_site snapshot-server:80 cookie snapshot_site

backend servers
  cookie SERVERID insert indirect nocache
  server current_site server.com:80 cookie current_site weight 50
  server snapshot_site snapshot-server:80 cookie snapshot_site weight 50

We use this for staged rollouts, setting the weight in the servers backend to 0, and then testing using the query param current_site=true.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。