Skip to content

HAProxy ACLs and content switching

How HAProxy ACLs route requests by host, path, header, or method — with real examples and the order-of-evaluation rules that trip people up.

ACLs (Access Control Lists) are how HAProxy makes routing decisions in HTTP mode. They are named boolean tests over the request — “is the Host header api.example.com”, “does the path start with /admin”, “is the User-Agent a known bot” — that you reference from use_backend, http-request deny, http-request set-header, and many other rules. This article covers the common ACL patterns we use in production and the gotchas around evaluation order.

How to verify

If you suspect an ACL is not firing, three things to check: that the frontend is actually receiving the request, that the ACL definition matches what you think it matches, and that the rule order puts the right use_backend first.

sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo journalctl -u haproxy -n 100 --no-pager | grep -i acl
echo "show info" | sudo socat /run/haproxy/admin.sock -
curl -v -H 'Host: api.example.com' http://localhost/items

The verbose curl shows what HAProxy actually receives. If your ACL is hdr(host) -i api.example.com and the client sends Host: api.example.com:80, the match fails — that’s a real production failure pattern.

What’s happening

An ACL has two parts: a fetch method that pulls a piece of data from the request, and one or more patterns it is compared against. Syntax:

acl <name> <fetch_method> [flags] [operator] <pattern...>

Common fetch methods:

  • hdr(<name>) — the value of a request header.
  • path — the URI path, normalized.
  • path_beg / path_end / path_dir / path_sub — convenience matchers (begin, end, directory segment, substring).
  • url_beg, url_param(<name>) — URL prefix and query parameter.
  • method — the HTTP method (GET, POST, etc.).
  • src — the source IP.
  • ssl_fc_sni — the SNI value from the TLS handshake.

Flags modify matching: -i for case-insensitive, -m beg for prefix match (most matchers default to exact). Multiple patterns on one line mean OR; multiple ACLs on a use_backend mean AND. A ! negates an ACL.

Order of evaluation inside a frontend is top-to-bottom for http-request/http-response rules, and for use_backend the first matching line wins. default_backend is the fallback.

The procedure

  1. Route by Host header. Two domains, two backends, one frontend:

    frontend fe_http
        bind *:80
        acl is_api  hdr(host) -i api.example.com
        acl is_www  hdr(host) -i www.example.com example.com
        use_backend be_api if is_api
        use_backend be_www if is_www
        default_backend be_static

    The -i makes the comparison case-insensitive. The two patterns on is_www are an OR; either one matches.

  2. Route by path. API on /api/, admin on /admin/, app everywhere else:

    frontend fe_http
        bind *:80
        acl is_api    path_beg /api/
        acl is_admin  path_beg /admin/
        use_backend be_api   if is_api
        use_backend be_admin if is_admin
        default_backend be_app

    path_beg is anchored to the start of the path. path /api/users matches path_beg /api/. Use path_dir if you want segment-boundary matching (so /api/ matches but /apiv2/ does not).

  3. Combine ACLs. Route to a canary backend only for internal IPs on /api/:

    frontend fe_http
        bind *:80
        acl is_api      path_beg /api/
        acl is_internal src 10.0.0.0/8 192.168.0.0/16
        use_backend be_api_canary if is_api is_internal
        use_backend be_api        if is_api
        default_backend be_app

    Space between is_api and is_internal means AND. Order matters: the canary line is first because it is the narrower match.

  4. Block bad methods. Reject TRACE and OPTIONS at the frontend:

    frontend fe_http
        bind *:80
        acl bad_method method TRACE OPTIONS CONNECT
        http-request deny if bad_method
        default_backend be_app
  5. Route by header value. A common pattern for API versioning:

    frontend fe_http
        bind *:80
        acl api_v2 hdr(x-api-version) -i v2
        use_backend be_api_v2 if api_v2
        default_backend be_api_v1
  6. SNI routing in TCP mode. When you want to terminate TLS at the backend, route TCP frontends by SNI without decrypting:

    frontend fe_tls_passthrough
        bind *:443
        mode tcp
        tcp-request inspect-delay 5s
        tcp-request content accept if { req_ssl_hello_type 1 }
        acl is_api req_ssl_sni -i api.example.com
        use_backend be_api_tls if is_api
        default_backend be_app_tls

    The inspect-delay and the tcp-request content accept are required — without them HAProxy forwards the connection before reading the SNI.

  7. Test, reload, observe. Use real requests against 127.0.0.1 and inspect the log line — httplog mode includes the chosen backend in the line.

Common pitfalls

  • hdr(host) includes the port if the client sent one (api.example.com:443). Use hdr_beg(host) -i api.example.com if you want to match regardless of port, or match both variants explicitly.
  • path is the URL-decoded path; url is the original raw value. If you are matching against query strings or non-ASCII bytes, url_* matchers are what you want.
  • use_backend fires on the first match. A broad path_beg / ACL above a narrow path_beg /api/ ACL will always win — narrow first.
  • ACLs defined inside a frontend are not visible to a different frontend. If you need shared ACLs, define them in a userlist or use a common backend with its own internal ACLs.
  • http-request rules run before use_backend. If you http-request set-header X-Real-Host %[hdr(host)] and then ACL on hdr(x-real-host), the ACL works — but only because the rule above it ran first. Reordering breaks it silently.

Stack Harbor inherits the ACL trees clients have grown over years — overlapping rules, dead branches, duplicate use_backend lines — and folds them down to a maintainable set. We document each ACL with what it routes and why; that documentation is what survives the next person to inherit the config. This kind of cleanup is part of how we run Clustered Environments.