haproxy

How to match only one specific path in haproxy and redirect the request to a backend service


How can we redirect only one specific path request to a backend service in haproxy

Below is my haproxy.cfg file

global
  log 127.0.0.1   local1
  maxconn 4096
  ssl-default-bind-ciphers TLS13-AES-256-GCM-SHA384:TLS13-AES-128-GCM-SHA256:TLS13-CHACHA20-POLY1305-SHA256:EECDH+AESGCM:EECDH+CHACHA20
  ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11

defaults
  mode http
  maxconn 2048


frontend app1-fe
  bind *:80
  bind *:443 ssl crt /usr/local/etc/haproxy/test.pem
  errorfile 503 /usr/local/etc/haproxy/errors/index.http

  acl app2acl path_beg  /v2
  use_backend app2    if app2acl

  acl app1v2docs path_beg -M /v2/api-docs
  use_backend app1   if app1v2docs

  redirect scheme https if !{ ssl_fc }
  mode http
  timeout connect 5s
  timeout client 5s
  timeout server 5s
  default_backend app1

backend app1
  redirect scheme https if !{ ssl_fc }
  server app1 app1:8080  check inter 5s rise 2 fall 3
 
backend app2  
  server app2 app2:4173  check inter 5s rise 2 fall 3

Now I wanted to redirect just one specific endpoint/path(/v2/api-docs) to app1 of /v2/* endpoint series/pattern and rest of the paths/endpoints after /v2 to app2 application.

So how can we make a particular endpoint/path match in haproxy.cfg file?


Solution

  • Order matters. First matching rule applies, so change their order. More specific rules first:

      acl app1v2docs path_beg /v2/api-docs
      use_backend app1   if app1v2docs
    
      acl app2acl path_beg  /v2
      use_backend app2    if app2acl
    
    

    Also that -M is for loading map from a file, so i guess it was not needed.