Replacing a part of the path by another URL on haproxy

35 views Asked by At

I created an HAProxy to hide some API urls to the external users behind our URL (myurl.contoso.net/).

For example I have some internal API's:

API NAME URL Resolved by HAProxy as
API1 www.mysite.com/api/v1 myurl.contoso.net/api/v1
API2 www.mysite2.com/api/v1 myurl2.contoso.net/api/v1

I have no issues with this, because I can point these backends to the server.

And then we have another case where we use an external API but we want to convert to our URL.

API NAME URL Resolved by HAProxy as
API3 www.customersite.com/api/service/v1/ myurl3.contoso.net/api/service/v1/

But I'm getting issues when I try to redirect the URL from the customer site to my url.

I'm expecting when someone make a request on postman like: myurl3.contoso.net/api/service/v1/product-orders

My HAProxy rewrites into: www.customersite.com/api/service/v1/product-orders

I mean, I want to convert their domain for my domain, but the rest of the request on the url should be kept.

My config:

frontend mysite
        mode http
        option httplog
        bind *:80
        bind *:443 ssl crt /etc/ssl/hacerts/certificate.pem
        redirect scheme https if !{ ssl_fc }


use_backend API1 if { hdr(host) -i myurl1.contoso.net }
use_backend API2 if { hdr(host) -i myurl2.contoso.net }
use_backend API3 if { hdr(host) -i myurl3.contoso.net }


backend API1
        mode http
        server node01 10.1.10.10:80 check


backend API2
        mode http
        server node01 10.1.10.20:80 check


backend API3 # (doesn't work)
        mode http
        http-request redirect location https://www.customersite.com/api/service/v1/

0

There are 0 answers