You add a security header to a site behind HAProxy, scoped to one hostname so it doesn't affect anything else on the same frontend:
http-response set-header X-Frame-Options SAMEORIGIN if { hdr(host) -i example.com }
The config reloads cleanly. Then you curl -I the site and the header isn't there. Not on the homepage, not on any page. Take the if off and it shows up everywhere. Put it back and it shows up nowhere.
The condition isn't almost right. It can't match at all, because by the time http-response rules run, the data it's testing isn't there. Here's why, and the fix.
The symptom#
The pattern is always the same:
- An
http-responserule (set-header,add-header,del-header, anything) has a condition that looks at the request: the Host header, a path, a request header. - The config passes validation and HAProxy reloads without errors.
- The rule never fires on real traffic. No error, no log line, nothing.
- The same condition works perfectly on an
http-requestrule.
That last point is what makes it confusing. You've probably written if { hdr(host) -i example.com } a hundred times on request rules and it has always worked.
The cause: request data isn't there at response time#
HAProxy processes a transaction in two phases. http-request rules run while the request is being handled, before it goes to the server. http-response rules run later, once the server's response has come back. Every sample fetch is tagged with the phases it's valid in, and the request-header fetches are only valid during request processing.
The manual hints at this in a few places. capture.req.method is documented as working "in both request and response because it's allocated", unlike method. In other words, request data that hasn't been explicitly captured or copied somewhere isn't something you can count on during response processing.
Then there's the hdr() name itself, which is ambiguous in two different ways:
- As a sample fetch (for example inside
%[...]or aset-varexpression),hdr()is direction-relative. The manual says it's "equivalent to req.hdr() when used on requests, and to res.hdr() when used on responses." So%[hdr(host)]in a response rule reads the response's Host header, which normally doesn't exist. - As an ACL keyword (the
{ hdr(host) -i example.com }form),hdrmaps toreq.hdr, the request-only fetch. In a response rule, that's a fetch HAProxy has already flagged as unusable there.
Either way you're not testing the original request's Host header. Writing req.hdr(host) explicitly doesn't help, because req.hdr is exactly the fetch that isn't valid during response processing.
http-response condition that came from the request has to be saved during the request phase, into something that survives into the response phase.Broken vs. fixed configs#
Broken: request fetch in a response condition
frontend fe_main
bind :80
default_backend be_app
# Never matches. hdr in an ACL is req.hdr, which isn't valid here.
http-response set-header X-Frame-Options SAMEORIGIN if { hdr(host) -i example.com }
# Never matches either, same reason, just spelled explicitly.
http-response set-header X-Content-Type-Options nosniff if { req.hdr(host) -i example.com }
Broken, and silent: hdr() as a fetch in a response rule
# Reads the RESPONSE's Host header (normally absent), not the request's.
http-response set-header X-Debug-Host %[hdr(host)]
Fixed: save it to a txn variable during the request
frontend fe_main
bind :80
default_backend be_app
# Request phase: normalize the Host header (lowercase, strip any :port)
# and store it in a transaction-scoped variable.
http-request set-var(txn.host) req.hdr(host),lower,regsub(:[0-9]+$,)
# Response phase: test the variable, not the header.
http-response set-header X-Frame-Options SAMEORIGIN if { var(txn.host) -m str example.com }
http-response set-header X-Content-Type-Options nosniff if { var(txn.host) -m str example.com }
The scope matters. HAProxy variables come in several scopes, and the manual is precise about their lifetimes:
txn: lives for the whole transaction. This is the one you want.req: visible only "until the first server connection attempt." Gone before the response arrives.res: visible only "from the first server connection attempt." Too late to set from the request.sess: lives for the whole session, which can carry several requests over a keep-alive connection. That's more than you need for a per-request value.
The manual is explicit that there is "no overlap at all between 'req' and 'res' variables." A req.host variable is simply gone by the time response rules look for it.
Alternative: a header capture
If you already capture the Host header for logging, you can reuse it. capture.req.hdr(<idx>) reads a value saved by a capture request header line, and it's allowed during response processing:
frontend fe_main
capture request header Host len 64
http-response set-header X-Frame-Options SAMEORIGIN if { capture.req.hdr(0),lower -m str example.com }
The index is the position of the capture line in the frontend, starting at 0. A captured value is the raw header, port included if the client sent one, so the txn variable with normalization is usually the cleaner choice.
Don't borrow someone else's variable#
Once you know you need a txn variable, it's tempting to look through the config, find one that already holds the hostname, and reuse it. Be careful with that, especially if the variable lives in a namespace another component owns.
The main example is SPOE, the protocol HAProxy uses to talk to external agents like WAFs. Agents write their results back as HAProxy variables under a configured prefix (e.g. txn.<prefix>.<name>), and the SPOE protocol includes both set-var and unset-var actions. If you park your own value inside an agent's prefix, you're sharing space with code you don't control.
The safe pattern is boring: set a variable whose only job is your rule, with a name nothing else uses, right next to the rule that reads it.
How to debug it yourself#
1. Read the warnings, not just the exit code
haproxy -c -f /etc/haproxy/haproxy.cfg
HAProxy does catch the ACL form of this mistake at parse time. A request-only fetch in an http-response condition produces a warning along the lines of:
parsing [haproxy.cfg:12] : 'anonymous acl will never match because it uses keyword 'hdr' which is incompatible with 'frontend http-response header rule''.
But it's a warning, not an error, so the check still passes and the reload still goes through. If your config is generated by automation that only looks at pass/fail, nobody ever sees it. HAProxy has a global zero-warning option that makes it refuse to start on any warning. It's worth considering on configs that don't change often.
The sample-fetch form, %[hdr(host)], gets no warning at all, because hdr() is legitimately valid in both phases. It just reads the wrong side.
2. Echo the value back in a debug header
Find out what your condition actually sees at response time by temporarily writing it into a response header:
http-response set-header X-Debug-Host "%[var(txn.host,NOTSET)]"
The second argument to var() is a default returned when the variable isn't set, so you can tell "unset" apart from "set to something unexpected." Then:
curl -sI https://example.com/ | grep -i x-debug-host
If you see NOTSET, the variable didn't survive to the response phase (wrong scope, wrong name, or something else cleared it). If you see a hostname you didn't expect (with a port, or uppercase), fix the normalization. Remove the debug header when you're done.
3. Put it in the log line
For traffic you can't reproduce with curl, add %[var(txn.host)] to your log-format and compare it against the requests where the header went missing.
4. Test responses, not configs
"The config validated" and "the rule fires" are separate claims, and only the second one matters. After any change to response rules, check real responses on more than one path. Our free Security Headers Checker does that against any public URL, and Reading Your Security Headers Scan covers what each result means.
5. Remember which responses http-response sees
Even with a correct condition, http-response rules only apply to responses that come from a server. The manual notes that http-after-response rules, unlike http-response, also apply to "all responses generated by HAProxy" (redirects, error pages, denies). If a header shows up on normal pages but not on HAProxy's own 403 or redirect, that's why.
How we hit this#
We ran into this while building per-domain security headers at our edge. Each customer's site gets its own set of response headers, so every rule needs a host condition on a shared frontend.
Every rule our edge had generated before then was a request-side rule, where hdr(host) is exactly right. So the first version of the feature used the same host condition we'd used everywhere else. The config was valid and reloaded cleanly. Then we tested real HTTP responses through the edge, and none of the headers were there.
We isolated it with debug headers like the ones above. First we confirmed that both hdr(host) and req.hdr(host) come up empty in the response phase. Then we tried the obvious shortcut: the config already had an unconditional txn variable holding the normalized hostname, set for our WAF integration and living under the WAF agent's SPOE prefix. Same expression, already there. A debug header showed it reading back empty at response time, every time we tested.
We didn't pin down exactly what cleared it. A new variable with its own name and the identical expression worked on every test, so that's what shipped. The lesson we took from it: every response-side rule gets its own txn variable, and "the config deployed" never counts as "the feature works" until a real response says so.
Check what your responses actually send
Run any public URL through the free Security Headers Checker. On ShieldIngress, you can fix what's missing with one click, with no code changes and no HAProxy rules to write yourself.