4

I have a Haproxy instance that rewrites Host headers to internal ones using http-request set-header.

http-request set-header Host internal.example

However, I'd still like backends to have access to the original Host header. I think X-Forwarded-Host is a good candidate for this, however I'm struggling to figure out how to do that.

I'm using this, which works:

capture request header Host len 64
http-request set-header X-Forwarded-Host %hrl

But of course that's a giant hack - won't work for more than 64 chars and won't work if I ever need to capture another header.

Is there a better way?

  • Why are you changing these headers? – Joel E Salas Dec 17 '15 at 23:05
3

As Joel E Salas has mentioned via a comment, changing the Host header is a slight strange thing to do, but I suppose if the backends only understand certain values it could make sense.

In any event, you can just use the req.hdr fetch sample as a variable to http-request, thusly:

http-request set-header X-Forwarded-Host %[req.hdr(Host)]

You'll have to run that before you run the http-request set-header that rewrites the Host header, but it should work.

  • Thanks! Setting this variable allows us to do further processing on an IIS back-end site based on the value of the original host header (another domain or subdomain that was an alias for a back-end server). It comes through to IIS as HTTP_X_FORWARDED_HOST, which can be used as a conditional match pattern in the IIS rewrite rule. We had been using just HTTP_HOST in the rewrite rule prior to fronting it with haproxy. – Mark Oct 17 '16 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.