Many times there is a need to publish multiple websites from internal network, but there is only one public IP address available.

How this can be done easy way? HAProxy can help us with it.

In example configuration I have 2 URLs registered to same public IP address:

  • first.laboratory.net
  • second.laboratory.net
  • third.laboratory.net

Here is how HAProxy configuration for given example looks like…

/etc/haproxy/haproxy.cfg

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

frontend MAIN
    bind *:80
    mode http
    
    acl FIRST_URL hdr_dom(host) -i first.laboratory.net
    acl SECOND_URL hdr_dom(host) -i second.laboratory.net
    acl THIRD_URL hdr_dom(host) -i third.laboratory.net
    
    use_backend FIRST if FIRST_URL
    use_backend SECOND if SECOND_URL
    use_backend THIRD if THIRD_URL

backend FIRST
    mode http
    server web-first 10.1.1.1:80
    
backend SECOND
    mode http
    server web-second 10.1.1.2:80
    
backend THIRD
    mode http
    server web-third 10.1.1.3:80

All requests to external URLs will be redirected to appropriate internal servers.

Enabling HAProxy Statistics

For monitoring/tracking purposes it might be also useful to enable statistics in HAProxy configuration.

listen stats AAA.BBB.CCC.DDD:8989
        mode http
        stats enable
        stats uri /stats
        stats realm HAProxy\ Statistics
        stats auth admin:admin

Above part has to be added to /etc/haproxy/haproxy.cfg configuration file.

Once it will be added refer to:

http://AAA.BBB.CCC.DDD:8989/stats

enter username admin and password admin to display HAProxy statistics.

Of course AAA.BBB.CCC.DDD has to be replaced with appropriate IP address of HAProxy server on which statistics web page should be available.