HAProxy Enterprise Documentation 1.6r2
Rewrite Requests
Use the http-request
configuration directives to rewrite HTTP requests. You can place it into a frontend
, listen
or backend
section. Use them to rewrite requests sent from clients before HAProxy Enterprise forwards them to a backend server. The server will see something different than what the client sent.
Change the HTTP request method
Use http-request set-method
to change the method (e.g. GET, POST, PUT) on a request before relaying it to a backend server. In the example below, we change GET requests made to the /login URL path to be POST requests:
frontend www
bind :80
acl url_login path_beg -i /login
http-request set-method POST if METH_GET url_login
use_backend webservers
As another example, turn a GET into a POST if there is data in the body of the request by checking the Content-Length header:
frontend www
bind :80
acl body_has_data req.hdr_val(Content-Length) gt 0
http-request set-method POST if METH_GET body_has_data
use_backend webservers
Set the URL path
Use http-request set-path
to change the requested URL path before relaying it to a backend server. Below, we change the URL path for JPG images so that it begins with /images/, but only if not already set:
frontend www
bind :80
acl p_ext_jpg path_end -i .jpg
acl p_folder_images path_beg -i /images/
http-request set-path /images/%[path] if !p_folder_images p_ext_jpg
use_backend webservers
This changes the requests below:
GET /images/flower.jpg HTTP/1.1
GET /daisy.jpg HTTP/1.1
... into the following ones, respectively:
GET /images/flower.jpg HTTP/1.1
GET /images/daisy.jpg HTTP/1.1
Set the query string
Use http-request set-query
to change the requested URL's query string. You can use the query
fetch method to get the current query string value. Then use the regsub
function to replace the first occurrence of a given substring. In the example below, we replace the string %3D with = in the query string. The third parameter is set to g, which applies the replacement to all occurences within the string:
frontend www
bind :80
http-request set-query %[query,regsub(%3D,=,g)]
use_backend webservers
Youc an also use the urlp
fetch method to get one of the query string's parameters. Below, we reorder the parameters in the query string to ensure that the user parameter comes before the group parameter:
frontend www
bind :80
http-request set-query user=%[urlp(user)]&group=%[urlp(group)]
use_backend webservers
This changes the request below:
GET /test.php?group=admin&user=foo HTTP/1.1
...into the following one:
GET /test.php?user=foo&group=admin HTTP/1.1
Set the URI
Use http-request set-uri
to rewrite the entire URI string of an HTTP request, including its HTTP scheme, authority, path, and query string. In the example below, we create one URL for HTTP and another for HTTPS when forwarding traffic to a proxy server:
backend b_squid
acl https ssl_fc
http-request set-uri https://%[req.hdr(Host)]%[path]?%[query] if https
http-request set-uri http://%[req.hdr(Host)]%[path]?%[query] unless https
# servers list...
This changes the request below:
GET /test.php?group=admin&user=foo HTTP/1.1
Host: www.domain.com
...into this one if the user connected over HTTP:
GET http://www.domain.com/test.php?group=admin&user=foo HTTP/1.1
...or into this one if the user connected over HTTPS:
GET https://www.domain.com/test.php?group=admin&user=foo HTTP/1.1
Rewriting anywhere in the request using regexes
The legacy statements reqrep
and reqirep
are still useful in cases not yet covered by the http-request
directives.
reqrep <search> <replace> [<cond>] reqirep <search> <replace> [<cond>]
reqirep <search> <replace> [<cond>] reqirep <search> <replace> [<cond>]
This directive needs a regular expression to <search>
the content to replace, a regular expression <replace>
to apply the modification , and an optional <condition>
to apply to this rule.
Note
HAProxy Enterprise uses PCRE compatible regular expressions.
Below, we replace /jpg/ with /images/ in the URL path:
frontend www
bind :80
reqrep ^([^ ]*) /jpg/(.*) 1 /images/2
use_backend webservers
Next, we rewrite the Host header from static.domain.com to www.domain.com:
frontend www
bind :80
acl h_static hdr(Host) -m beg static.domain.com
reqirep ^Host: static.domain.com Host: www.domain.com if h_static
use_backend webservers