7

Using HAProxy, I want to create the following setup:

  • All requests except root (/), /articles and /blogs go to server1
  • All requests for root (/), /articles and /blogs go to server2

I can't figure out how to match root without relying on setting the default server, which then negates the all requests go to server1 rule.

How can I express the above using HAProxy?

A regex may be the solution to this, but I'm not great with regex so it has been difficult to come up with a solution based on it.

michaelward82
  • 251
  • 2
  • 7

1 Answers1

7

The answer to this was astoundingly simple of course. The ACL needed to regex match ^$|^/$|^/articles|^/blogs

Below is my conf:

global
  pidfile  /var/run/haproxy.pid
  quiet
  daemon

defaults
  mode  http
  option  httplog
  option  dontlognull
  option http-server-close
  retries 1
  maxconn 1024
  contimeout  15000
  clitimeout  60050
  srvtimeout    1200000

frontend www
  bind :80

  acl is_for_server2 path_reg ^$|^/$|^/articles|^/blogs

  use_backend server2 if is_for_server2

  default_backend server1

backend server1
  option forwardfor
  server server1 10.0.8.1 maxconn 1500

backend server2
  option forwardfor
  server server2 10.0.8.2 maxconn 1500
jjmontes
  • 3,387
  • 2
  • 19
  • 27
michaelward82
  • 251
  • 2
  • 7
  • a request for /blogstuff would go to server2, whereas OP *may* have intended such a request would go to server1. To fix, the path_reg could be expanded to: ^$|^/$|^/articles/|^/articles$|^/blogs/|^/blogs$ –  Nov 05 '14 at 12:47
  • acl is_for_server2 path_reg ^$|^/$|^/web doesn't work for me. If /web then this regex is true, else it is false even when the url is www.domain.com or www.domain.com/ – Allen King Dec 18 '17 at 07:28