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

Darin Dimitrov
  • 223
  • 1
  • 2
  • 8
  • 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 09 '14 at 01:21

1 Answers1

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]
Nick Craver
  • 823
  • 7
  • 15
  • 1
    Thanks for confirming my conclusion that this is not possible in 1.4. I have switched to `nginx`. – Darin Dimitrov Jun 12 '14 at 07:04
  • %[url] is not always the full URL, but it does appear to be the best haproxy can do. haproxy 1.8 URL variable docs: http://cbonte.github.io/haproxy-dconv/1.8/configuration.html#7.3.6-url – zie May 15 '18 at 23:08