Score:0

curl: (7) Failed connect to 192.168.169.128:80; Connection refused, how to fix?

qa flag

Why is my haproxy load balancer not working?

I'm on centos 7.

I've set up 2 servers "nginx-node01" and "nginx-node02". As the name implies these 2 servers are of nginx. They're up and running.

The server's IP address being 192.168.169.133(hostname=nginx-node01) and 192.168.169.135(hostname=nginx-node02) respectively.

Then I've set up haproxy on another server. It's IP address being 192.168.169.128(hostname=haproxy-centos8). (I'm on centos 7, ignore the hostname centos8)

When I do curl 192.168.169.128 in haproxy server, I get:

curl: (7) Failed connect to 192.168.169.128:80; Connection refused

This is what I've done so far:

  1. Configure the /etc/hosts files in haproxy server.

vi /etc/hosts and add the following lines there

   192.168.169.128 haproxy-centos8
   192.168.169.133 nginx1.loadbalancer.me  nginx-node01
   192.168.169.135 nginx2.loadbalancer.me  nginx-node02

Save the file and exit the editor.

  1. Go to hosts file on /etc/hosts on nginx-node01 and paste the following lines:

     192.168.169.128  haproxy-centos8
     192.168.169.133  nginx-node01
     192.168.169.135  nginx-node02
    

Do the same for nginx-node02

  1. Go to haproxy-centos8 server. Edit haproxy.cfg file like this:

     #---------------------------------------------
     # Global settings
     #---------------------------------------------
     global
         log         127.0.0.1 local2     #Log configuration
    
         chroot      /var/lib/haproxy
         pidfile     /var/run/haproxy.pid
         maxconn     4000                
         user        haproxy             #Haproxy running under user and group "haproxy"
         group       haproxy
         daemon
    
         # turn on stats unix socket
         stats socket /var/lib/haproxy/stats
    
     #---------------------------------------------
     # common defaults that all the 'listen' and 'backend' sections will
     # use if not designated in their block
     #---------------------------------------------
     defaults
         mode                    http
         log                     global
         option                  httplog
         option                  dontlognull
         option http-server-close
         option forwardfor       except 127.0.0.0/8
         option                  redispatch
         retries                 3
         timeout http-request    10s
         timeout queue           1m
         timeout connect         10s
         timeout client          1m
         timeout server          1m
         timeout http-keep-alive 10s
         timeout check           10s
         maxconn                 3000
    
     #---------------------------------------------
     #HAProxy Monitoring Config
     #---------------------------------------------
     listen haproxy3-monitoring *:8080                #Haproxy Monitoring run on port 8080
         mode http
         option forwardfor
         option httpclose
         stats enable
         stats show-legends
         stats refresh 5s
         stats uri /stats                             #URL for HAProxy monitoring
         stats realm Haproxy\ Statistics
         stats auth howtoforge:howtoforge            #User and Password for login to the monitoring dashboard
         stats admin if TRUE
         default_backend app-main                    #This is optionally for monitoring backend
    
     #---------------------------------------------
     # FrontEnd Configuration
     #---------------------------------------------
     frontend main
         bind *:80
         option http-server-close
         option forwardfor
         default_backend app-main
    
     #---------------------------------------------
     # BackEnd roundrobin as balance algorithm
     #---------------------------------------------
     backend app-main
         balance roundrobin                                     #Balance algorithm
         option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost    #Check the server application is up and healty - 200 status code
         server nginx-node01 192.168.169.133:80 check                 #Nginx1 
         server nginx-node02 192.168.169.135:80 check                 #Nginx2
    
  2. Then configure rsyslog for HAProxy.

Edit this file

vi /etc/rsyslog.conf

Uncomment this line to enable the UDP connection:

$ModLoad imudp
$UDPServerRun 514

If you want to use a specific IP, you can add a new line like the one below:

$UDPServerAddress 127.0.0.1

Save the file and exit

  1. Then create new haproxy configuration file for rsyslog:

    cd /etc/rsyslog.d/ vi haproxy.conf

Paste configuration below:

local2.=info     /var/log/haproxy-access.log    #For Access Log
local2.notice    /var/log/haproxy-info.log      #For Service Info - Backend, loadbalancer

Then I did things like restarting rsyslog, starting haproxy, enabling haproxy etc. My nginx was already configured.

in flag
`connection refused` indicates that nothing is listening on that port. Use `ss -tnlp` to check if haproxy is actually running and listening on the correct interface. Further, your information does not match up. Sometimes you have `192.168.169.X`, sometimes `192.168.1.X`. Make sure you have the correct subnet configured everywhere.
achhainsan avatar
qa flag
That's a typo, I've edited the post now.
achhainsan avatar
qa flag
`users:(("haproxy",pid=1049,fd=5)) LISTEN 0 128 *:111 *:* ` This is the output of ss -tnlp
in flag
It is not listening on port 80 or 8080. You are probably editing the wrong config file.
achhainsan avatar
qa flag
I'm 100% sure I'm not editing the wrong conf file. I've done these steps multiple times again and again!
achhainsan avatar
qa flag
@GeraldSchneider I did nothing and it fixed itself.
vidarlo avatar
ar flag
You did something. You may not know what, but I'm rather certain it's something.
Reishin avatar
sa flag
the another thing is - why use haproxy for LB, nginx can do that no problem for http or tcp using configurable strategies. Back to the question, before posting the question it's better to analyze logs from the systemd service unit and application log itself. (in this case, haproxy) Is it even started and not crashed? apparmor? Firewall?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.