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

Haproxy: trouble using reqrep & use-server rules together

· 52CE 我爱测

I am trying to consolidate several backend blocks into one using use-server rules. I have requests for webadmin.example.org with various URL paths going to the same backend, & am trying to get these requests to go to different se…

问题原文

原帖链接 · Henry Clayton · CC BY-SA 4.0

I am trying to consolidate several backend blocks into one using use-server rules. I have requests for webadmin.example.org with various URL paths going to the same backend, & am trying to get these requests to go to different servers based on the URL paths. It is partially working, but failing in some cases. Haproxy 1.8.8 on Ubuntu 18.04. Here is my configuration:

backend webadmin
  # Tried both of these; it balances between servers otherwise.
  #balance source
  balance hdr(host)
  acl path:phpmyadmin path_beg -i /phpmyadmin
  acl path:phppgadmin path_beg -i /phppgadmin
  acl path:rabbitmq path_beg -i /rabbitmq
  acl path:rcmdr path_beg -i /rediscommander
  reqrep ^([^\ ]*\ /)rabbitmq[/]?(.*) \1\2 if path:rabbitmq
  reqrep ^([^\ ]*\ /)rediscommander[/]?(.*) \1\2 if path:rcmdr
  use-server rcmdr:00 if path:rcmdr
  use-server rabbitmq:00 if path:rabbitmq
  use-server nginx:00 if path:phpmyadmin || path:phppgadmin
  # Tried these too.
  #use-server rabbitmq:00 if { path_beg -i /rabbitmq }
  #use-server nginx:00 if { path_beg -i /phpmyadmin } || { path_beg -i /phppgadmin }
  #use-server rcmdr:00 if { path_beg -i /rediscommander }
  server rabbitmq:00 localhost:15672 maxconn 10000 check fall 3 rise 2
  server rcmdr:00 localhost:6670 maxconn 10000 check fall 3 rise 2
  server nginx:00 localhost:6669 maxconn 10000 check fall 3 rise 2

What I experience is that requests for path:phpmyadmin and path:phppgadmin matching ACLs go to the proper server (Nginx, where the URL paths are sorted out to the correct applications), but requests for the other two ACLs seem to ignore the use-server rules, & just go to whatever server is defined first in the list of rules (or if using a round-robin balance mode, every request is routed to a different backend, ignoring the use-server rules).

If I eliminate the reqrep rules, every request is routed properly according to the use-server rules. Unfortunately, I need these rules so that the backend servers respond to requests properly (i.e., they shouldn't see the subpath URLs because they don't know anything about them, they are just being used to route within Haproxy).

So, can I use the reqrep & use-server rules together? If so, how?

已采纳解答

原帖链接 · Henry Clayton · CC BY-SA 4.0

Well, I rigged up something that works for me using variables. As @Michael - sqlbot mentioned, I had assumed ACLs were similar to pass-by-value variables, so that you could stash values in there & use them even if the source of the value changes. And I wasn't aware that Haproxy had a set-var feature. I also looked at Lua options but opted for this since it does what I need it to do.

frontend public
  # ... bind, basic setup, etc.
  # ACLS by request path.
  acl path:rcmdr path_reg ^/rediscommander(/.*)?$
  acl path:rabbitmq path_reg ^/rabbitmq(/.*)?$
  # Set the variables here.
  http-request set-var(txn.rcmdr) bool(true) if path:rcmdr
  http-request set-var(txn.rabbitmq) bool(true) if path:rabbitmq
  acl dom:webadmin hdr_dom(host) -i webadmin.example.org
  use_backend webadmin if dom:webadmin

backend webadmin
  balance hdr(host)
  acl path:phpmyadmin path_beg -i /phpmyadmin
  acl path:phppgadmin path_beg -i /phppgadmin
  # Set the ACL if the variable was set in the frontend.
  acl path:rabbitmq var(txn.rabbitmq),bool
  acl path:rcmdr var(txn.rcmdr),bool
  use-server rcmdr if path:rcmdr
  use-server rabbitmq if path:rabbitmq
  use-server nginx if path:phpmyadmin || path:phppgadmin
  # reqrep doesn't kill our ACLs because they are set based on boolean variables
  # from the frontend, not from mutable request paths tested in the backend.
  reqrep ^([^\ ]*\ /)rabbitmq[/]?(.*) \1\2
  reqrep ^([^\ ]*\ /)rediscommander[/]?(.*) \1\2
  server rabbitmq localhost:15672 maxconn 10000 check fall 3 rise 2
  server rcmdr localhost:6670 maxconn 10000 check fall 3 rise 2
  server nginx localhost:6669 maxconn 10000 check fall 3 rise 2

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

资料来源与适用范围

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

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

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