HAProxy request rewrite

haproxy

I am trying to setup pass proxy with HAProxy.

Version :- HA-Proxy version 1.7.5 2017/04/03

I have two URL abc.com/foo and abc.com/bar and I want it to get redirected to my backend servers which are serving request as 10.0.0.1/xyz/

For example, if my requests are coming as abc.com/foo/login, so this request should go to my backend server as 10.0.0.1:7003/xyz/login but the front end should only show abc.com/foo/login.

I tried using below

 "http-request redirect code 301 location http://%[hdr(host)]%[url,regsub(^/foo/,/bar/,)] if { path_beg /foo }"

but it is changing the URL as abc.com/xyz

I have been using nginx as of now, below is my configuration of the same.

server {
   listen 8443;
        underscores_in_headers on;

location /Recharge {
        proxy_pass http://backend/RetailerApp;
        proxy_read_timeout 15;
}

location /Gateway {
        proxy_pass http://backend/RetailerApp;
        proxy_read_timeout 15;
}
}

upstream backend_preprod {
   server 10.5.214.237:7005;
   server 10.5.214.237:7004;
}

Best Answer

You don't want to use redirect because that will redirect the client to the given URL. You only need to rewrite the request URL.

reqrep ^([^\ :]*)\ /foo/(.*)     \1\ /xyz/\2
reqrep ^([^\ :]*)\ /bar/(.*)     \1\ /xyz/\2

https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#reqrep

Client will still see the /foo or /bar path, this will only rewrite it in communication between the proxy and the backend server.