--- toc: yes --- # Howto HAProxy [Documentation officielle](http://haproxy.1wt.eu/download/1.5/doc/configuration.txt). ## Installation Sous Debian Jessie, la version proposée est la 1.5.8 et 1.6.9 en backports. ~~~ # apt install haproxy ~~~ ## Configuration La configuration se passe dans le fichier `haproxy.cfg` se trouvant dans `/etc/haproxy` sous Debian. Voici un exemple de configuration : ~~~ global log /dev/log local5 log /dev/log local5 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin stats timeout 30s user haproxy group haproxy daemon # Default SSL material locations ca-base /etc/ssl/certs crt-base /etc/ssl/private # Default ciphers to use on SSL-enabled listening sockets. # For more information, see ciphers(1SSL). This list is from: # https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/ ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS ssl-default-bind-options no-sslv3 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 default-server port 80 maxconn 250 on-error fail-check slowstart 60s inter 1m fastinter 5s downinter 10s weight 100 listen stats bind *:8080 stats enable stats uri /haproxy stats show-legends stats show-node stats realm Auth\ required stats auth foo:bar stats admin if TRUE frontend myfront option forwardfor maxconn 800 bind 0.0.0.0:80 default_backend myback backend myback balance roundrobin server web01 192.0.2.1:80 check observe layer4 weight 100 server web02 192.0.2.2:80 check observe layer4 weight 100 server web03 192.0.2.3:80 check observe layer4 weight 100 ~~~ Pour activer les logs en mode debug, notamment utilisé pour voir **toutes** les requêtes, il faut remplacer notice par debug. ~~~ log /dev/log local5 debug ~~~ Attention, il faut donc que le démon `syslog` ait un paramétrage sur la facilité `local5` (ou autre selon votre configuration). Pour `rsyslog` cela se fait ainsi dans `rsyslog.conf` : ~~~ local5.* -/var/log/haproxy.log ~~~ ### Exemple de configuration avancée * Gestion de différents backend en fonction du domaine * Association d'un serveur à un utilisateur à l'aide d'un cookie (« sticky session ») ~~~ global [...] defaults [...] frontend http-in bind *:8080 # On définit des ACL qui associe un Host: HTTP à un backend acl is_domain1 hdr_end(host) -i domain1.example.com acl is_domain2 hdr_end(host) -i domain2.example.com use_backend domain1 if is_domain1 use_backend domain2 if is_domain2 default_backend domain1 backend domain1 # Avec cette directive, HAProxy ajoute automatiquement un cookie SERVERID aux réponses HTTP, # et l'utilise pour sélectionner le bon serveur lors de la prochaine requête cookie SERVERID insert indirect balance roundrobin # Pour ce serveur, la valeur du cookie SERVERID sera "web01" (directive "cookie") server web01 192.0.2.1:80 cookie web01 check # Pour ce serveur, la valeur du cookie SERVERID sera "web02" server web02 192.0.2.2:80 cookie web02 check backend domain2 cookie SERVERID insert indirect balance roundrobin server web01 192.0.2.1:80 cookie web01 check server web02 192.0.2.2:80 cookie web02 check ~~~ ### HTTP basic authentication Pour mettre en place une authentification HTTP basique au niveau d'HAProxy, définir dans la section globale une liste d'utilisateur, soit avec un mot de passe en clair soit avec un mot de passe chiffré : ~~~ userlist NomDeMaUserList user user1 insecure-password passwordEnClair user user2 password $6$passwordSHA512 […] ~~~ Dans le backend concerné rajouter : ~~~ auth AuthOkayPourMonSite http_auth(NomDeMaUserList) http-request auth realm Texte if !AuthOkayPourMonSite ~~~