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
-
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_staticThe
-imakes the comparison case-insensitive. The two patterns onis_wwware an OR; either one matches. -
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_apppath_begis anchored to the start of the path.path /api/usersmatchespath_beg /api/. Usepath_dirif you want segment-boundary matching (so/api/matches but/apiv2/does not). -
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_appSpace between
is_apiandis_internalmeans AND. Order matters: the canary line is first because it is the narrower match. -
Block bad methods. Reject
TRACEandOPTIONSat the frontend:frontend fe_http bind *:80 acl bad_method method TRACE OPTIONS CONNECT http-request deny if bad_method default_backend be_app -
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 -
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_tlsThe
inspect-delayand thetcp-request content acceptare required — without them HAProxy forwards the connection before reading the SNI. -
Test, reload, observe. Use real requests against
127.0.0.1and inspect the log line —httplogmode includes the chosen backend in the line.
Common pitfalls
hdr(host)includes the port if the client sent one (api.example.com:443). Usehdr_beg(host) -i api.example.comif you want to match regardless of port, or match both variants explicitly.pathis the URL-decoded path;urlis the original raw value. If you are matching against query strings or non-ASCII bytes,url_*matchers are what you want.use_backendfires on the first match. A broadpath_beg /ACL above a narrowpath_beg /api/ACL will always win — narrow first.- ACLs defined inside a
frontendare not visible to a differentfrontend. If you need shared ACLs, define them in auserlistor use a common backend with its own internal ACLs. http-requestrules run beforeuse_backend. If youhttp-request set-header X-Real-Host %[hdr(host)]and then ACL onhdr(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.