Search code examples
aws-lambdagoogle-cloud-functionsazure-functionshaproxy

HAProxy redirect based on the selected server


I'm trying to setup HAProxy to change the destination URL depending on the Server that gets picked by the loadbalancer, I have 3 webservices each one deployed in each of the services AWS, GCP and AZURE

  • The AWS is located at address1.com/service
  • The AZR is located at address2.com/bucketName/service
  • The GCP is located at address3.com/api/service

But in HAProxy I can't put a dash / on the server address to force a request made to example.com/helloWorld to go, for example, to address3.com/api/helloWorld, which is what I need, I want HAProxy to pick one of the servers for me using the balance method provided, and then have it calling the correct webservice depending on the server that was picked.

frontend example.com
    bind 0.0.0.0:80
    use_backend back_farm
    default_backend back_farm
backend back_farm
    mode http
    balance roundrobin
    option httpclose
    option forwardfor
    server awsback address1.com/
    server azrback address2.com/bucketName/
    server gcpback address3.com/api/

I tried creating an ACL to check the selected server, but it doesn't seem to recognize the %s argument mentioned on the docs, and I've neither found a better replacement for it.

backend back_farm
    mode http
    balance roundrobin
    option httpclose
    option forwardfor
    server awsback address1.com
    server azrback address2.com
    server gcpback address3.com

    acl is_azr %s -i azrback
    acl is_gcp %s -i gcpback

    http-request redirect code 301 location http://%[hdr(host)]%[url,regsub(^/service,/api/service,)] if is_azr 
    http-request redirect code 301 location http://%[hdr(host)]%[url,regsub(^/service,/bucketName/service,)] if is_gcp

What am I doing wrong in here, or what other way could I do this, to force the address to be changed depending on the destination server? Any ideas or guidance?


Solution

  • Even throught this isn't 100% a reply to the question, because I had to switch from HAProxy to NGINX to be able to achieve what I intended, I wanted to leave the solution I found here for others that might find it usefull.

    I was able to achieve this using the following configuration:

    upstream backend.example.com {
        server aws.backend.example.com:8091;
        server azr.backend.example.com:8092;
        server gcp.backend.example.com:8093;
    }
    
    server {
        listen 80;
        listen [::]:80;
        server_name backend.example.com;
        location / {
            proxy_pass http://backend.example.com;
        }
    }
    server {
        listen 8091;
        listen [::]:8091;
        server_name aws.backend.example.com;
        location / {
            proxy_pass http://address1.com:80/;
        }
    }
    server {
        listen 8092;
        listen [::]:8092;
        server_name azr.backend.example.com;
        location / {
            proxy_pass http://address2.com:80/api/;
        }
    }
    server {
        listen 8093;
        listen [::]:8093;
        server_name gcp.backend.example.com;
        location / {
            proxy_pass http://address3.com:80/bucketName/;
        }
    }