HAProxy — load balancer dla aplikacji webowych
Opublikowano: 9 kwietnia 2026 · Kategoria: VPS / Infrastruktura
HAProxy (High Availability Proxy) to jeden z najbardziej wydajnych i niezawodnych load balancerów open source. Obsługuje miliony połączeń dziennie w infrastrukturach firm takich jak GitHub, Airbnb czy Twitter. Oferuje aktywne health checks, zaawansowane algorytmy, szczegółowe statystyki i SSL termination — wszystko w lekkim procesie, który zużywa ułamek zasobów w porównaniu z innymi rozwiązaniami.
Instalacja HAProxy
# Ubuntu / Debian (repozytoria HAProxy.org dla najnowszej wersji) apt-get install -y software-properties-common add-apt-repository ppa:vbernat/haproxy-2.8 apt-get update apt-get install -y haproxy=2.8.\* # Sprawdzenie wersji haproxy -v # Status usługi systemctl status haproxy
Struktura konfiguracji — cztery sekcje
Plik konfiguracyjny HAProxy (/etc/haproxy/haproxy.cfg) składa się z czterech
sekcji:
- global — parametry procesu (użytkownik, logi, maksymalna liczba połączeń)
- defaults — domyślne wartości dla frontendów i backendów
- frontend — nasłuchiwanie na porcie, reguły routingu
- backend — lista serwerów docelowych, algorytm, health checks
Podstawowa konfiguracja HTTP load balancera
# /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
maxconn 50000
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
option forwardfor # dodaj X-Forwarded-For
option http-server-close # keep-alive do klientów
frontend http_front
bind *:80
default_backend app_servers
# Przekierowanie HTTPS
# redirect scheme https if !{ ssl_fc }
backend app_servers
balance roundrobin
server app1 10.0.0.10:3000 check
server app2 10.0.0.11:3000 check
server app3 10.0.0.12:3000 check # Weryfikacja konfiguracji haproxy -c -f /etc/haproxy/haproxy.cfg # Restart / reload systemctl reload haproxy
Algorytmy load balancingu
| Algorytm | Dyrektywa | Kiedy używać |
|---|---|---|
| Round-robin | balance roundrobin | Krótkie, równomierne requesty; backendy tej samej mocy |
| Least connections | balance leastconn | Długie połączenia (WebSocket, DB proxy, upload plików) |
| Source hash | balance source | Sticky sessions — klient zawsze trafia na ten sam backend |
| URI hash | balance uri | Cache affinity — ten sam URL na ten sam serwer |
| Random | balance random | Duże klastry (10+ serwerów) — mniejszy overhead niż roundrobin |
Aktywne health checks
Kluczowa przewaga HAProxy nad darmowym Nginx: aktywne health checks — HAProxy regularnie pyta backendy o stan i nie czeka na błąd w ruchu produkcyjnym:
backend app_servers
balance roundrobin
# HTTP health check — GET /health musi zwrócić 2xx
option httpchk GET /health HTTP/1.1\r\nHost:\ app.example.com
# Parametry health check
default-server inter 3s rise 2 fall 3
# inter 3s = sprawdzaj co 3 sekundy
# rise 2 = 2 sukcesy = serwer zdrowy (UP)
# fall 3 = 3 błędy = serwer wyłączony (DOWN)
server app1 10.0.0.10:3000 check weight 1
server app2 10.0.0.11:3000 check weight 1
server app3 10.0.0.12:3000 check weight 2 # 2x więcej ruchu
# Serwer backup — tylko gdy wszystkie główne DOWN
server app-backup 10.0.0.99:3000 check backup Strona statystyk (stats page)
HAProxy oferuje wbudowany dashboard statystyk — bez dodatkowych narzędzi:
listen stats
bind *:8404
stats enable
stats uri /haproxy-stats
stats realm HAProxy\ Statistics
stats auth admin:bezpieczne-haslo
stats refresh 5s # auto-odświeżanie co 5 sekund
stats show-legends
stats show-node
Otwórz http://twoj-serwer:8404/haproxy-stats — zobaczysz tabelę z każdym serwerem,
statusem (UP/DOWN/MAINT), liczbą aktywnych sesji, requestów i błędów. Zabezpiecz port 8404 firewallen
— tylko z zaufanych IP.
SSL termination
# Połącz cert + klucz w jeden plik (wymóg HAProxy)
cat /etc/letsencrypt/live/example.com/fullchain.pem \
/etc/letsencrypt/live/example.com/privkey.pem \
> /etc/haproxy/certs/example.com.pem
chmod 600 /etc/haproxy/certs/example.com.pem
# Konfiguracja SSL w frontend
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
bind *:80
http-request redirect scheme https unless { ssl_fc }
# Nagłówki bezpieczeństwa
http-response set-header Strict-Transport-Security "max-age=31536000"
http-response set-header X-Frame-Options SAMEORIGIN
default_backend app_servers HAProxy vs Nginx — kiedy co wybrać
| Cecha | HAProxy | Nginx (open source) |
|---|---|---|
| Aktywne health checks | Tak (wbudowane) | Nie (tylko Nginx Plus) |
| Serwowanie plików statycznych | Nie | Tak |
| Statystyki | Wbudowana strona stats | stub_status (ograniczone) |
| Tryb TCP proxy | Tak (mode tcp) | Tak (stream module) |
| Algorytmy LB | 8+ algorytmów | 5 algorytmów |