11

I have the following setup:

                 |-------|--- backend1:8080
example.com:80---|HAProxy|--- backend2:8080
                 |-------|--- backend3:8080

I would like to configure the HAProxy to add a custom header containing the original url. For example if the user requests http://example.com/foo?bar=baz, i.e.:

GET /foo?bar=baz HTTP/1.1
Host: example.com:80

I would like the HAProxy to translate this request to the backend like so:

GET /foo?bar=baz HTTP/1.1
Host: backend1:8080
x-custom-header: http://example.com/foo?bar=baz

The reqadd option only allows me to add a header with a static value. I was able to get the path portion from the request using reqrep:

reqrep ^([^\ ]*)\ ([^\ ]*)\ (HTTP\/1\.[10])  \0\r\nx-custom-header:\ \2

but unfortunately this captures only the path portion of the original url resulting in the following header:

x-custom-header: /foo?bar=baz

So my question is whether it is possible in HAProxy 1.4 to combine both the Host header and the first line of the HTTP request into a custom header

  • Have you tried combining reqadd with an acl that references the be_id (backend-id)? It's not precisely dynamic but it might get you closer if the number of backends is small... reqirep also supports an optional acl, so you could possibly write separate regexes for each back-end, each active only if an acl matches the selected backend-id. – Michael - sqlbot May 9 '14 at 1:21
18

Short answer: no, you can't do this in HAProxy 1.4.


However, for those finding this question on 1.5+:

In HAProxy 1.5+ you can reference variables via the %[variable] syntax and you're not restricted to only using static strings. You can see which variables exist already in section 7.3 of the HAProxy configuration guide (specifically 7.3.6 for layer 7 variables).

For your case, you're looking at raw url, like this:

http-request set-header X-Custom-Header %[url]

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.