How to Set Up HAProxy as a Load Balancer

Setting Up HAProxy as a Load Balancer

HAProxy distributes incoming traffic across multiple backend servers to improve availability and performance.

Install HAProxy

apt update
apt install haproxy -y

Basic Configuration

nano /etc/haproxy/haproxy.cfg
global
    log /dev/log local0
    maxconn 4096

defaults
    log global
    mode http
    timeout connect 5s
    timeout client 30s
    timeout server 30s

frontend http_in
    bind *:80
    default_backend webservers

backend webservers
    balance roundrobin
    option httpchk GET /health
    server web1 192.168.1.101:80 check
    server web2 192.168.1.102:80 check
    server web3 192.168.1.103:80 check

Balance Algorithms

  • roundrobin — distribute requests evenly in rotation
  • leastconn — send to the server with fewest active connections (best for long-lived connections)
  • source — sticky sessions by client IP

Enable the Stats UI

listen stats
    bind *:8404
    stats enable
    stats uri /stats
    stats auth admin:password

Access at http://YOUR_SERVER_IP:8404/stats.

Start and Enable HAProxy

systemctl enable haproxy
systemctl start haproxy

SSL Termination at HAProxy

frontend https_in
    bind *:443 ssl crt /etc/ssl/certs/yourdomain.pem
    default_backend webservers

Combine your certificate and key into one PEM file: cat fullchain.pem privkey.pem > yourdomain.pem

  • 0 Користувачі, які знайшли це корисним
Ця відповідь Вам допомогла?

Схожі статті

Understanding Your Server's Network Configuration

Understanding Your Server's Network Configuration Knowing how to read and manage your server's...

Setting Up Reverse DNS (rDNS/PTR Record)

Setting Up Reverse DNS (rDNS / PTR Record) A reverse DNS (PTR) record maps your IP address back...

How to Configure a Static IP Address on Linux

Configuring a Static IP Address on Linux Dedicated servers typically have a static IP configured...

Setting Up Nginx as a Reverse Proxy

Setting Up Nginx as a Reverse Proxy A reverse proxy forwards incoming requests to a backend...

How to Configure DNS Records for Your Domain

Configuring DNS Records for Your Domain DNS records tell the internet how to reach your domain....