i have to make a loadbalancer for two servers hosted on a hostserver. When i surf to my site it gives a 503 error "Service Unavailable No server is available to handle this request."
Does someone knows a fix for this?
this is my loadbalancer task
--- - name: install HAproxy apt: name: haproxy state: latest - name: copy config template: src: haproxy.j2 dest: /etc/haproxy/haproxy.cfg notify: - restart haproxy - name: open the listening port ufw: rule: allow port: '80' proto: tcp notify: - restart ufw This is my haproxy template
global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats # utilize system-wide crypto-policies ssl-default-bind-ciphers PROFILE=SYSTEM ssl-default-server-ciphers PROFILE=SYSTEM 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 frontend http bind *:{{ portloadbalancer }} default_backend webservers backend webservers balance roundrobin server {{ webservernaam1 }} {{ ipwebserver1 }}:{{ portloadbalancer }} server {{ webservernaam2 }} {{ ipwebserver2 }}:{{ portloadbalancer }} backend app balance roundrobin server {{ webservernaam1 }} {{ ipwebserver1 }}:{{ portloadbalancer }} check server {{ webservernaam2 }} {{ ipwebserver2 }}:{{ portloadbalancer }} check This is my nginx_default_conf
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; } } the ansible playbook runs it with no problem, also when i watch the logs on my webservers (/var/log/nginx/error.log) there isn't any attempt to connect recorded.
Maybe another interesting output 
1 Answer
The journalctl shows that you are getting permission denied while binding to port 80; note that port 80 is privileged and requires root permissions for binding. You are running the haproxy as a non-root user from the last snippet. Once, you use root user, you might need to address the following:
Also, You have used bind: *:80, meaning binding on port 80 for "all" the interfaces. At the same time, you are the same ports for backend servers(again, port 80). This is causing the connection refused error.
This error has nothing to do with ansible, but the issue is with haproxy configuration. See example configuration HERE(CTRL +F 2.6. Examples) . In summary, You have two options,
- Either use different ports for the bind and backends. (for example, 80 and 8080), either by changing
haproxy.cfgornginx.cfg. - Bind to a specific interface IP, not
*.The following are twonetstatoutputs:
#Example-1: bind to all, same as your current configuration
Here traffic to any IP address assigned to the machine on port 80 will be load balanced.
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13836/haproxy #Example-2: when only bind to a single Ip. Here I am only binding a single interface to port 80
So, only the traffic coming to the 192.168.1.240 on port 80 will be load balanced.
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 192.168.1.240:80 0.0.0.0:* LISTEN 14178/haproxy 21