HAProxy Umsetzung

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen

Installation

apt install haproxy

Zertifikat vorbereiten

HAProxy benötigt Zertifikat und Key in einer einzigen PEM-Datei.

mkdir -p /etc/haproxy/ssl
cat /etc/ssl/own.crt /etc/ssl/own.key > /etc/haproxy/ssl/reproxy.pem
chmod 600 /etc/haproxy/ssl/reproxy.pem

Konfiguration

Die Konfiguration befindet sich in /etc/haproxy/haproxy.cfg.

nano /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 expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
    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:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
    ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
    ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

# Frontend: HTTPS
frontend ft_https
    bind 10.88.2XX.41:443 ssl crt /etc/haproxy/ssl/reproxy.pem
    mode http
    option http-keep-alive
    option forwardfor
    timeout client 30s
    acl acl_reproxy hdr_beg(host) -i reproxy
    use_backend backend_www if acl_reproxy

# Frontend: HTTP → HTTPS Redirect
frontend ft_http
    bind 10.88.2XX.41:80
    mode http
    option http-keep-alive
    timeout client 30s
    acl acl_http req.proto_http
    http-request redirect code 301 scheme https if acl_http

# Backend: www.it2XX.int
backend backend_www
    server www 10.88.2XX.YY:443 ssl verify none check

Konfiguration testen

haproxy -c -f /etc/haproxy/haproxy.cfg

Die Ausgabe sollte lauten:

Configuration file is valid

Haproxy Erläuterungen

HAProxy starten / neu laden

systemctl enable haproxy
systemctl restart haproxy

Bei laufendem Betrieb reicht ein Reload, damit bestehende Verbindungen nicht unterbrochen werden:

systemctl reload haproxy

Test

Vom Client aus:

curl -k http://reproxy.it2XX.int
curl -vk https://reproxy.it2XX.int

Der erste Aufruf sollte einen 301 Redirect auf HTTPS liefern, der zweite die Seite von www.it2XX.int.