Faire un serveur HAProxy de base sous Linux
On est ici sur une AlmaLinux 9.2, une RedHat like donc, mais cela doit certainement s'adapter facilement pour Ubuntu et autre.
La documentation officielle se trouve sur https://www.haproxy.com/documentation/hapee/latest/configuration/config-sections/
L'installation se fait simplement avec
yum update-minimal
dnf install haproxy
Lien vers le fichier : cliquez ici
Aller dans le répertoire d'haproxy et sauvegarder la configuration par défaut
cd /etc/haproxy
cp haproxy.cfg bak_haproxy.cfg
Lien vers le fichier : cliquez ici
Lancer le service
systemctl enable haproxy
systemctl start haproxy
systemctl status haproxy
Lien vers le fichier : cliquez ici
Pour que SELinux n'empêche pas HAProxy d'accéder aux sicket TCP, tapez
setsebool -P haproxy_connect_any=1
Lien vers le fichier : cliquez ici
Vous pouvez commencer à éditer la configuration avec nano par exemple
nano /etc/haproxy/haproxy.cfg
Lien vers le fichier : cliquez ici
Une configuration très basique avec juste l'écoute sur le port 80 et une redirection sur un serveur mavmnumero2 192.168.0.35:80 serait
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# Frontend HTTP
#---------------------------------------------------------------------
frontend frontend-http
bind *:80
default_backend web_servers
backend web_servers
mode http
server mavmnumero2 192.168.0.35:80 check
Lien vers le fichier : cliquez ici
Le fichier de configuration peut être vérifié avec
haproxy -f /etc/haproxy/haproxy.cfg -c
Lien vers le fichier : cliquez ici
Une modification de configuration peut être prise en compte à chaud avec
systemctl reload haproxy
systemctl status haproxy
Lien vers le fichier : cliquez ici
Comme expliqué sur https://www.haproxy.com/blog/exploring-the-haproxy-stats-page
Vous pouvez activer une page affichant des statistiques en ajoutant le bloc ci-dessous.
#---------------------------------------------------------------------
# Stats #
#---------------------------------------------------------------------
frontend stats
mode http
bind *:9090
stats enable
stats uri /stats
stats refresh 10s
stats admin if LOCALHOST
Lien vers le fichier : cliquez ici
Pages Web
| Site Web | Description |
|---|---|
| it-connect.fr audit haproxyanssi | Résultat de l'audit de l'anssi sur haproxy |