1

I currently have a structure with an Apache 2 server with multiple subdomains. As the example below:

<VirtualHost *:80>
 ServerName www.example.com
 ServerAlias example.com
 DocumentRoot /var/www/main
</VirtualHost>

<VirtualHost *:80>
 ServerName subdomain-1.example.com
 ServerAlias subdomain-1.example.com
 DocumentRoot /var/www/sbd1
</VirtualHost>

<VirtualHost *:80>
 ServerName subdomain-2.example.com
 ServerAlias subdomain-2.example.com
 DocumentRoot /var/www/sbd2
</VirtualHost>

I am considering duplicating this server and just creating a new one with HAProxy to perform load balancing.

Does HAProxy support point all requests to the child server with the same IP or for each subdomain will I need specific server with a specific IP?

Tom
  • 289
  • 3
  • 13
  • Yes, it is possible. What do you want from HAProxy? What problem are you trying to solve? – Mircea Vutcovici Jan 16 '20 at 02:26
  • Partially yes. HAproxy as proxy is able to handle SNI (more domains on the same port). The example you have provided is not related to proxy case where HAproxy is great tool but to http server case where the HAproxy is not tool for it. You can use HAproxy to check the request but you still need http server to provide the content for the answer... Often combination is haproxy + nginx. See the answer for the example for this case related to haproxy config. – Kamil J Jan 16 '20 at 08:30
  • @MirceaVutcovici I am planning to use HAProxy to perform load balancing between servers. – Tom Jan 16 '20 at 11:50

2 Answers2

1

You will need to configure backends for each subdomain, or possibly use a default backend if most requests should reach a specific server. If you configure multiple backends, there’s nothing preventing them from pointing at the same web server.

Mikael H
  • 5,031
  • 2
  • 9
  • 18
1

I am afraid this is not really what you want / need. HAproxy is great tool to act as proxy* but the use case you are providing as sample is not related to proxy but regular http server*. In case you want to replace apache the option could be e.g. nginx.


proxy - application which doing check of the request and / or do subrequests to provide the content

http server - application providing the content


It is possible to use HAproxy as frontend and nginx as backend even on the same node. The configuration od HAproxy would be something like this (without https as requested):

frontend http_front
    maxconn 1000
    mode http
    option http-server-close
    option forwardfor
    bind *:80

    acl top_domain hdr(host) -m str www.example.com
    acl top_domain hdr(host) -m str example.com

    acl subdom1 hdr(host) -m str www.subdomain-1.example.com
    acl subdom1 hdr(host) -m str subdomain-1.example.com

    acl subdom2 hdr(host) -m str www.subdomain-2.example.com
    acl subdom2 hdr(host) -m str subdomain-2.example.com


    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto http if !{ ssl_fc }

    use_backend local if subdom1
#   ...
    default_backend local

backend local
    fullconn 1000
    mode http
    timeout http-keep-alive 3000
    server local1 127.0.0.1:8080 check maxconn 100

There can be more backends ending on different servers... I have just follow the example ;-).

Kamil J
  • 1,632
  • 1
  • 5
  • 10
  • I am planning to use HAProxy to perform load balancing between servers. But since I don't want to create multiple servers for each subdomain, I want to leave them on the same http server as the main application. – Tom Jan 16 '20 at 11:51
  • 1
    At that moment you don't need SNI support on HAproxy. You can just setup frontend even without any ACL and use just ``default_backend``... SNI ("multi domain support") can be then handle on backend - http server (nginx?). – Kamil J Jan 16 '20 at 20:30