I'm using HAProxy to load balance the incoming requests on my two tomcat backend servers. In the config file when I specify HAProxy to listen to the same machine on which the HAProxy is installed, everything is fine and it starts to work like a charm. I need HAProxy to listen to a virtual IP on another machine (so that I can install another LB as failover), but when I config HAProxy to do so, I get this error on starting HAProxy:

cannot bind socket for proxy http_proxy. Aborting.

here is my config file:

    maxconn 4096 # Total Max Connections. This is dependent on ulimit
    nbproc 4 
    mode http
    clitimeout 60000
    srvtimeout 30000
    contimeout 4000
    option httpclose # Disable Keepalive

listen http_proxy #the virtual IP I want to listen to
    mode http
    stats enable
    stats auth xxx:xxx
    balance source # Load Balancing algorithm
    option httpchk
    option forwardfor # This sets X-Forwarded-For
    ## Define your servers to balance
    server web1 weight 1 maxconn 512 check
    server web2 weight 1 maxconn 512 check

I'm using CentOS 5.1 installed on some virtual machines. It's necessary to say that I completely stopped the iptables service on the remote machine and the problem is still there. I suppose something is interfering with haproxy in the remote machine. Any Ideas?

  • Hello SJ.Jafari, So did you got solution to this problem ? – Ciasto piekarz Dec 30 '20 at 21:45

You can also allow a process to bind to a non-local (i.e., non-existent IP) by adding net.ipv4.ip_nonlocal_bind=1 to /etc/sysctl.conf and running sysctl -p. But what sysadmin1138 is valid: you may want to look at how you've architected your failover.

  • Non-local bind! THAT'S What I was trying to remember. – sysadmin1138 Apr 7 '12 at 13:35
  • @cjc: I see what you're saying buddy. I gave it a shot and it didn't work because the IP I'm trying to listen to does exist, but HAProxy can't listen to it. Something is blocking the process I suppose. – SJ.Jafari Apr 9 '12 at 9:55

When doing a failover config like you say you want, I've found that it's better to let the clustering mechanism handle the moving of IP addresses rather than somehow doing it in HAProxy. You configure the virtual IP address to move between nodes, and then configure it to have a service (HAproxy) follow that virtual IP. You then configure HAProxy to only listen on the virtual IP. This works because it's the clustering software that starts and stops the HAproxy process as it moves around, you don't do it from the normal services.

  • Thanks for the answer, I see your point and I'm kinda confused! I thought something like heartbeat would just make sure about high availability of my LBs and my plan was to install it when I needed to add another LB. But it seems there is more about these clustering softwares, could u guide me more on this? What specific tool should I use? What steps should I take? (Sorry I'm sort of a noob to the technology) – SJ.Jafari Apr 7 '12 at 13:06
  • @SJ.Jafari You'd use Heartbeat to make the LB highly available. If you need more than one load-balancer running at the same time, things get a lot more complicated. – sysadmin1138 Apr 7 '12 at 13:38
  • Well, that's what I thought. But the problem isn't that right now, the problem is HAProxy won't listen to another IP. It's kinda an OS ish difficulty I think. – SJ.Jafari Apr 8 '12 at 9:28
  • 1
    @SJ.Jafari look at my Answer about the kernel setting to do a non-local bind. – cjc Apr 8 '12 at 12:11

You are getting the error because haproxy tries to listen on non-existing IP address. It seems that you are starting haproxy process before VIP get assigned to the machine.

One solution is to listen on all interfaces You can then restrict access to the VIP using iptables if needed.

Another option is to listen on the VIP, but you need to make sure the VIP exists when starting haproxy. This can be done by letting the clustering software (such as keepalived) start haproxy for you when it acquires the VIP.

  • I'm afraid to disagree with your scenario, because that IP IS assigned to a machine in the network and I can ping to that machine from the LB before starting HAProxy. – SJ.Jafari Apr 7 '12 at 12:47

See the above answers about setting net.ipv4.ip_nonlocal_bind=1.

If that doesn't work, check if you already have an existing service listening on the port you're trying to bind to, with sudo lsof -i :80.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.