На главную
DevOps и серверы
Конфиг HAProxy и Traefik
Одна форма — два балансировщика: haproxy.cfg или конфиги Traefik v3 (статический, динамический и метки docker-compose). Домены, бэкенды с весами и резервом, проверки здоровья, sticky-сессии, HTTPS с Let's Encrypt, HSTS, лимит запросов с IP и панель статистики.
Сайт app
Пусто — без проверок
Бэкенды
HTTPS и защита
0 — без лимита
Таймауты и статистика
/etc/haproxy/haproxy.cfg
global
log /dev/log local0
maxconn 50000
user haproxy
group haproxy
daemon
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor
timeout connect 5s
timeout client 60s
timeout server 60s
timeout http-request 10s
frontend http_in
bind :80
acl acme path_beg /.well-known/acme-challenge/
use_backend certbot if acme
http-request redirect scheme https code 301 unless acme
frontend https_in
bind :443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1
stick-table type ip size 100k expire 30s store http_req_rate(10s)
http-request track-sc0 src
http-request deny deny_status 429 if { sc_http_req_rate(0) gt 200 }
http-request set-header X-Real-IP %[src]
http-request set-header X-Forwarded-Proto https
http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains"
acl host_app hdr(host) -i app.example.com
use_backend be_app if host_app
default_backend be_app
backend be_app
balance roundrobin
option httpchk
http-check send meth GET uri /health ver HTTP/1.1 hdr Host app.example.com
http-check expect status 200
server s1 10.0.0.11:3000 check inter 5s fall 3 rise 2 weight 1
server s2 10.0.0.12:3000 check inter 5s fall 3 rise 2 weight 1
server s3 10.0.0.13:3000 check inter 5s fall 3 rise 2 weight 1 backup
backend certbot
server certbot 127.0.0.1:8888
listen stats
bind 127.0.0.1:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:CHANGE_ME
Сертификат и запуск
sudo mkdir -p /etc/haproxy/certs sudo certbot certonly --standalone --http-01-port 8888 -d app.example.com -m admin@example.com --agree-tos \ --deploy-hook 'cat "$RENEWED_LINEAGE/fullchain.pem" "$RENEWED_LINEAGE/privkey.pem" > "/etc/haproxy/certs/$(basename "$RENEWED_LINEAGE").pem" && systemctl reload haproxy' sudo haproxy -c -f /etc/haproxy/haproxy.cfg # проверка синтаксиса sudo systemctl reload haproxy ssh -L 8404:127.0.0.1:8404 сервер # затем http://localhost:8404/stats
Предупреждения
- Предупреждение
Пароль панели статистики пустой — в конфиге будет заглушка CHANGE_ME, замените её.