HAProxy
Siehe auch
Anweisungen alphabetisch in Config-Files zu implementieren funktioniert nur teilweise. use_backend-Regeln werden der Reihe nach abgearbeitet; die erste zutreffende Regel gewinnt, und Anfragen werden an das dazugehörige Backend weitergeleitet.
Erweiterte Features bedingen teils LUA-Scripting, welches in den RHEL-/CentOS-Binaries nicht hineincompiliert ist.
Versionen Stand 2020-11:
- CentOS 7: 1.5 (ohne LUA-Support) 
- CentOS 8: 1.8 (ohne LUA-Support) 
- Fedora Server 33: 2.2.6 (inkl. LUA-Support) 
- Links
- Doku zur Config, z.B. zur Version 2.4: http://www.haproxy.org/download/2.4/doc/configuration.txt 
 
Installation
yum -y install haproxy
# show version info
haproxy -vv
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.orig
SELinux - wenn HAProxy zu anderen Ports als 80/443 verbinden soll:
setsebool -P haproxy_connect_any on
Konfiguration prüfen/checken:
haproxy -c -V -f /etc/haproxy/haproxy.cfg
systemctl enable haproxy
systemctl start haproxy
X.509-Zertifikate (SSL/TLS)
Ein Zertifikat muss in folgender Reihenfolge zusammengebaut werden:
- CA 
- Intermediates 
- Server-Cert 
- Private Key 
Logging
UDP-Empfang konfigurieren:
# older versions
$ModLoad imudp
$UDPServerRun 514
# newer versions
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
local2.* /var/log/haproxy.log
systemctl restart rsyslog
tail -f /var/log/haproxy.log
Stats
Statistiken analog Apaches „server-status“:
#---------------------------------------------------------------------
# stats http://myip:9000/server-status
#---------------------------------------------------------------------
listen stats # Define a listen section called "stats"
    bind :9000 # Listen on localhost:9000
    mode http
    stats enable  # Enable stats page
    stats hide-version  # Hide HAProxy version
    stats realm HAproxy\ Statistics  # Title text for popup window
    stats uri /server-status  # Stats URI
    stats auth haproxy-stats:mypassword  # Authentication credentials
Aufruf: http://:9000/server-status; maschinenlesbar mit http://:9000/server-status;csv.
Was die einzelnen Werte bedeuten siehe https://www.haproxy.com/blog/exploring-the-haproxy-stats-page/.
Redirect HTTP > HTTPS
frontend frontend-http
    mode http
    bind :80
    http-request redirect scheme https code 301 unless { ssl_fc }
Header-Modifikation
frontend www
    bind :80
    # Add a response header
    http-response add-header X-XSS-Protection "1; mode=block"
    # Remove a response header
    http-response del-header X-Powered-By
    # Change a response header
    http-response set-header Via "HTTP/2.0 haproxy1"
    default_backend webservers
CORS
Bei Meldungen der Art Access to fetch at ‘http://api.example.com/dothing’ from origin ‘http://example.com’ has been blocked by CORS policy. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. muss eine CORS-Policy eingerichtet werden. CORS definiert das Whitelisting von Domains, die sonst von der Same-Origin-Policy des Browsers blockiert werden würden. Das geschieht per Access-Control-Allow-Origin Header.
Um das im HAProxy umzusetzen, wird ein LUA-Modul benötigt. Unglücklicherweise sind die alten HA-Proxy-Versionen auf RHEL/CentOS 7+ nicht mit LUA-Support compiliert - hier hilft aber der Einsatz eines Fedora-Servers.
CORS LUA-Modul laden und aktivieren:
cd /tmp
VER=2.2
wget https://github.com/haproxytech/haproxy-lua-cors/archive/v$VER.tar.gz
tar xf v$VER.tar.gz
cp haproxy-lua-cors-$VER/lib/cors.lua /etc/haproxy
global
    # LUA-module implementing CORS
    lua-load /etc/haproxy/cors.lua
Anschliessend das Abfangen der Request- und Response-Headers durch das LUA-Script in der frontend oder listen-Section einfügen. Die Konfigurationsangaben benötigen ab v2.0 des Moduls die Parameter allowed_methods, allowed_origins, allowed_headers.
v2.0+:
listen api
    bind :80
    http-request lua.cors "GET,PUT,POST" "example.com,localhost,localhost:8080" "X-Custom-Header1,X-Custom-Header2"
    http-response lua.cors
listen api
    bind :80
    http-request lua.cors "*" "*" "*"
    http-response lua.cors
v1.0:
listen api
    bind :80
    http-request lua.cors
    http-response lua.cors "GET,PUT,POST" "example.com,localhost,localhost:8080"
Siehe auch
Keycloak (OAuth)
HAProxy ist nicht in der Lage, wie der Apache eine Authentifizierung via OAuth (per Modul, LUA etc.) vorzunehmen. Hier muss man sich anderweitig behelfen, z.B. mit Hilfe des Programms oauth2-proxy.
Built on 2025-10-27