Switch from NGINX to HAProxy

I am trying to change load balancing, which I am using, the main purpose I need this for using Docker Swarm’s DNSRR endpoint mode which nginx cannot resolve.

I’ve such nginx config:

server {

    listen 8080;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # To forward the original client's IP address
    proxy_set_header X-Forwarded-Proto $scheme; # to forward the  original protocol (HTTP or HTTPS)
    proxy_set_header Host $http_host; # to forward the original host requested by the client
    proxy_set_header X-Real-IP $remote_addr;


    location /swagger/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;

        proxy_pass http://swagger-merger/;
    }
    
    location /rabbitmq/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
    
        proxy_pass http://rabbitmq:15672/;
    }

    location /companies/ {
        proxy_http_version 1.1;

        proxy_pass http://companies-api/;
    }

    location /links/ {
        proxy_http_version 1.1;

        proxy_pass http://links-api/;
    }

    location /news/ {
        proxy_http_version 1.1;

        proxy_pass http://news-api/;
    }

    location /importstructure/ {
        proxy_http_version 1.1;

        proxy_pass http://import-structure-api/;
    }

    location /structure/ {
        proxy_http_version 1.1;

        proxy_pass http://structure-api/;
    }

    location /profile/ {
        proxy_http_version 1.1;

        proxy_pass http://profile-api/;
    }

    location /registration/ {
        proxy_http_version 1.1;

        proxy_pass http://registration-api/;
    }

    location /files/ {
        proxy_http_version 1.1;

        proxy_pass http://files-api/;
    }
}

What I need to make the same settings with HAProxy config. What is nginx’s location directive analogue in HAProxy? My HAProxy config:

global
    log          fd@2 local2
    chroot       /var/lib/haproxy
    pidfile      /var/run/haproxy.pid
    maxconn      4000
    user         haproxy
    group        haproxy
    stats socket /var/lib/haproxy/stats expose-fd listeners
    master-worker

resolvers docker
    nameserver dns1 127.0.0.11:53
    resolve_retries 3
    timeout resolve 1s
    timeout retry   1s
    hold other      10s
    hold refused    10s
    hold nx         10s
    hold timeout    10s
    hold valid      10s
    hold obsolete   10s

defaults
    timeout connect 10s
    timeout client 30s
    timeout server 30s
    log global
    mode http
    option httplog

backend swagger
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template swagger-merger- 1 http://swagger-merger check resolvers docker init-addr libc,none

backend stat
    stats enable
    stats refresh 15s
    stats show-legends
    stats show-node

Is that right setup for backend “swagger-merger” which is first in nginx? Or did I miss something? I just need help with one, other I’ll do with analogue.

Thanks in advance for any help!

Please, halp.

Is that valid configuration?

global
    log     	 fd@2 local2
    chroot  	 /var/lib/haproxy
    pidfile 	 /var/run/haproxy.pid
    maxconn 	 4000
    user    	 haproxy
    group   	 haproxy
    stats socket /var/lib/haproxy/stats expose-fd listeners
    master-worker

resolvers docker
    nameserver dns1 127.0.0.11:53
    resolve_retries 3
    timeout resolve 1s
    timeout retry   1s
    hold other      10s
    hold refused    10s
    hold nx         10s
    hold timeout    10s
    hold valid      10s
    hold obsolete   10s

defaults
    timeout connect 10s
    timeout client 30s
    timeout server 30s
    log global
    mode http
    option httplog


listen swagger-merger
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template swagger-merger- 1 http://swagger-merger/ check resolvers docker init-addr libc,none

listen rabbitmq
    acl rabbitmq path_beg /api/rabbitmq/
    use_backend if rabbitmq
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template rabbitmq- 1 http://rabbitmq:15672/ check resolvers docker init-addr libc,none

listen companies
    acl companies path_beg /api/companies/
    use_backend if companies
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template companies-api- 1 http://companies-api/ check resolvers docker init-addr libc,none

listen links
    acl links path_beg /api/links/
    use_backend if links
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template links-api- 1 http://links-api/ check resolvers docker init-addr libc,none

listen news
    acl news path_beg /api/news/
    use_backend if news
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template news-api- 1 http://news-api/ check resolvers docker init-addr libc,none

listen profile
    acl profile path_beg /api/profile/
    use_backend if profile
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template profile-api- 1 http://profile-api/ check resolvers docker init-addr libc,none

listen files
    acl files path_beg /api/files/
    use_backend if files
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template files-api- 1 http://files-api/ check resolvers docker init-addr libc,none

listen registration
    acl registration path_beg /api/registration/
    use_backend if registration
    balance leastconn
    option httpchk
    retry-on all-retryable-errors
    retries 2
    server-template registration-api- 1 http://registration-api/ check resolvers docker init-addr libc,none

listen stat
    stats enable
    stats refresh 15s
    stats show-legends
    stats show-node

Hi,

Your configuration seems good.
I would just change init-addr to init-addr none,last. The libc value may prevent your HAProxy from starting up (or slow it down) if some hosts are missing or DNS does not answer fast enough.

You can try the config with haproxy -c -f yourfile.cfg