One IP Adddress, more web server for different web traffic

Meanwhile, happy new year to all friends on this forum.

I have never worked with HAProxy and now ask for advice and be directed to a guide to help me solve my problem; which is not having a load balancer, but distributing the web traffic on multiple Apache servers having only one IP address.
For example, in DNS I have domain1.tld and domain2.tld which refer to a single IP address.
However, domain1.tld is managed by the Apache-A server while domain2.tld is managed by the Apache-B server.
From what I understand I have to place HAproxy in front of the two servers, but I did not understand how to route traffic to one or the other.
HAProxy I would put it as a package of the pfSense firewall.

In reality, I will not only have two domains on two Apache servers, but several domains distributed on several servers in a different way.

Moreover:

  • some domains will have sub domains
  • domains and subdomains will have their own Let’s Encrypt certificate
  • some web applications will be reachable using a port number.

To give a concrete example:

  • domain1.tld -> Apache-A
  • prj.domain1.tld -> Apache-B
  • crm.domain1.tld -> Apache-A
  • erp.domain2.tld:1000 -> Apache-B
  • tools.domain1.tld/tools1 -> Apache-A
  • tools.domain1.tld/tools2:3000 -> Apache-A
  • tools.domain1.tld/tools3 -> Apache-A

Starting from scratch, today I don’t even know if a distribution as in the example is possible or if it should keep a domain and its subdomains on the same server.

I first read the documentation and probably misread, but I only find references to load balancing.

You need three main parts:

  • one or more frontends that tell haproxy on which ports to listen, which x509 cert+key to use, on which IPs to listen, which TLS protocols to use, etc.
  • one or more backends that tell haproxy which downstream servers to which it should send requests
    • In your case you’ll have two: Apache-A and Apache-B
  • the glue: rules that tell haproxy “when you see a request like this, use this backend”
    • In your case, something like:
      acl server_a hdr(host) -i domain1.tld
      …
      use_backend apache_a if server_a
      

I strongly recommend starting with HAProxy version 2.3.0 - Starter Guide and the examples therein. Looks especially for “HTTP reverse-proxy” which is what your setup it.