HAProxy cannot bind socket on nodejs webserver

i'm trying to use HAProxy to redirect on different nodejs webserver working on three different port. What i want is that when i try to open /node1 HAProxy redirect me to that web server. This is HAProxy configuration:

frontend dispatcher
    mode http
    bind *:80
    acl node_serv1 url_beg /node1
    acl node_serv2 url_beg /node2
    acl node_serv3 url_beg /node3

    use_backend backend_1 if node_serv1
    use_backend backend_2 if node_serv2
    use_backend backend_3 if node_serv3

backend backend_1
    mode http
    balance roundrobin
    option  httpchk
    server web01 127.0.0.1:3001 check


backend backend_2
    mode http
    balance roundrobin
    option  httpchk
    server web02 127.0.0.1:3002 check

backend backend_3
    mode http
    balance roundrobin
    option  httpchk
    server web03 127.0.0.1:3003 check

All three nodejs webserver are working but when i try to start haproxy i get this error:

Starting haproxy: [ALERT] 111/105652 (27208) : Starting frontend dispatcher: cannot bind socket [0.0.0.0:80]

looking on web someone suggest to add this property

 net.ipv4.ip_nonlocal_bind=1

on /etc/sysctl.conf

but the error persists and HAProxy doesn't start. What's wrong in my configuration?


Solution 1:

The most probable cause is that some other process is already bound to port 80 (e.g. another webserver).

Before starting haproxy I would suggest to check if port 80 is free to bind

nestat -anp | grep ":80"

If result is not empty than some process already "took" port 80 and your haproxy won't be able to bind to it (especially when you try to bind on all interfaces). Some solution is to stop this process, but this is up to you to verify if that is feasible, as there might another webpage running there.

Another cause is that you try to start the process as non-root user. Non-root users are usually not allowed to bind to privileged ports (with number lower than 1024).