HAProxy installieren
apt install -y haproxy
# Version
haproxy -v
# Status
systemctl status haproxy
Basis-Konfiguration
# /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
maxconn 50000
user haproxy
group haproxy
daemon
stats socket /run/haproxy/admin.sock mode 660 level admin
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
errorfile 400 /etc/haproxy/errors/400.http
errorfile 503 /etc/haproxy/errors/503.http
# Frontend: HTTPS eingehend
frontend https_frontend
bind *:443 ssl crt /etc/ssl/firma.de.pem
default_backend web_servers
# HTTP → HTTPS Redirect
bind *:80
redirect scheme https if !{ ssl_fc }
# ACLs (URL-basiertes Routing)
acl is_api path_beg /api/
use_backend api_servers if is_api
# Backend: Web-Server
backend web_servers
balance roundrobin
option httpchk GET /health
http-check expect status 200
server web-01 192.168.20.10:80 check inter 5s rise 2 fall 3
server web-02 192.168.20.11:80 check inter 5s rise 2 fall 3
server web-03 192.168.20.12:80 check backup # Nur bei Ausfall
# Backend: API-Server
backend api_servers
balance leastconn # Least Connections für API
option httpchk GET /api/health
server api-01 192.168.20.20:3000 check
server api-02 192.168.20.21:3000 check
Session-Stickiness
# Gleicher User → gleicher Server (für stateful Apps)
backend web_servers
balance roundrobin
cookie SERVERID insert indirect nocache
server web-01 192.168.20.10:80 check cookie web01
server web-02 192.168.20.11:80 check cookie web02
HAProxy Stats Dashboard
# Stats-Seite aktivieren
frontend stats
bind *:9090
stats enable
stats uri /stats
stats refresh 10s
stats realm HAProxy Statistics
stats auth admin:sicheres-passwort
stats show-legends
stats show-node
HAProxy Runtime-API
# Server deaktivieren (Maintenance ohne Neustart!)
echo "disable server web_servers/web-01" | socat stdio /run/haproxy/admin.sock
# Server wieder aktivieren
echo "enable server web_servers/web-01" | socat stdio /run/haproxy/admin.sock
# Server-Status prüfen
echo "show servers state web_servers" | socat stdio /run/haproxy/admin.sock
# Traffic-Statistiken
echo "show info" | socat stdio /run/haproxy/admin.sock
FAQ
Was ist der Unterschied zwischen HAProxy und Nginx als Load Balancer?
HAProxy: spezialisiert auf Load Balancing, mächtigere Health-Checks, bessere Performance bei reinem Proxy. Nginx: Zusätzlich Webserver und statische Dateien.
Fazit
HAProxy ist der Standard für hochverfügbare Web-Infrastruktur – einfache Konfiguration, exzellente Performance und zero-downtime Wartung über die Runtime-API.
Load Balancing und Hochverfügbarkeit für KMU in Heidelberg, Mannheim und der Rhein-Neckar-Region. Anfragen.