How to add custom header containing the absolute address of the originally requested resource

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


Solution 1:

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]