🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

HAProxy Load Balancing: Complete Setup Guide for Linux (2026)

HAProxy Load Balancing: Complete Setup Guide for Linux (2026)

Quick Summary: HAProxy (High Availability Proxy) is the industry-standard open-source load balancer and reverse proxy. It distributes incoming traffic across multiple backend servers, providing high availability, improved performance, and seamless scaling. HAProxy handles millions of connections per second and is used by companies like GitHub, Stack Overflow, and Instagram.

HAProxy load balancing architecture

What Is Load Balancing?

Load balancing distributes incoming network traffic across multiple servers to ensure no single server bears too much load. This improves application availability (if one server fails, others handle traffic), performance (requests are spread evenly), and scalability (add servers to handle more traffic).

Installing HAProxy

  • Debian/Ubuntu: sudo apt install haproxy
  • RHEL/AlmaLinux: sudo dnf install haproxy
  • Enable: sudo systemctl enable --now haproxy
  • Verify: haproxy -v

HAProxy Configuration Structure

SectionPurpose
globalProcess-wide settings (logging, max connections, user)
defaultsDefault settings for all frontends/backends
frontendDefines how requests are received (bind address, port)
backendDefines the pool of servers that handle requests
listenCombines frontend and backend in one section

Load Balancing Algorithms

AlgorithmDescriptionBest For
roundrobinDistributes requests evenly in orderEqual-capacity servers
leastconnSends to server with fewest connectionsLong-lived connections
sourceHash client IP for sticky sessionsSession-dependent apps
uriHash request URICache optimization
firstFill first server before using nextMinimize active servers

Health Checks

HAProxy continuously monitors backend server health:

  • TCP check (default): Verifies port is open
  • HTTP check: Sends HTTP request and checks response code
  • option httpchk GET /health — Check a specific health endpoint
  • http-check expect status 200 — Expect specific response
  • inter 5s fall 3 rise 2 — Check every 5s, mark down after 3 failures, up after 2 successes

SSL/TLS Termination

HAProxy can handle SSL termination, offloading encryption from backend servers:

  • Bind with SSL: bind *:443 ssl crt /etc/haproxy/certs/combined.pem
  • Redirect HTTP to HTTPS in frontend
  • Backend communication can remain HTTP (faster, simpler)

Monitoring with Stats Page

Enable the built-in statistics dashboard:

  • Add a listen stats section on a dedicated port (e.g., 8404)
  • Set stats enable, stats uri /stats, and stats auth admin:password
  • Access at http://server:8404/stats for real-time server and connection metrics

HAProxy vs NGINX (as Load Balancer)

FeatureHAProxyNGINX
Primary purposeLoad balancer/proxyWeb server + proxy
TCP load balancingExcellent (native)Available (stream module)
Health checksAdvanced (HTTP, TCP, custom)Basic (HTTP/TCP)
Stats dashboardBuilt-in, detailedRequires stub_status + tools
Connection handlingOptimized for proxyingOptimized for serving + proxying
SSL terminationExcellentExcellent

Frequently Asked Questions

How many connections can HAProxy handle?

HAProxy can handle millions of concurrent connections and hundreds of thousands of requests per second on modern hardware. It is used by some of the highest-traffic websites in the world. Performance depends on CPU (for SSL) and memory (for connection tracking).

What is the difference between Layer 4 and Layer 7 load balancing?

Layer 4 (TCP) load balancing routes based on IP address and port — it does not inspect the content. Layer 7 (HTTP) load balancing can inspect HTTP headers, URLs, and cookies to make routing decisions. HAProxy supports both modes.

Do I need HAProxy if I use NGINX?

For simple load balancing, NGINX is sufficient. HAProxy excels when you need advanced health checking, detailed statistics, TCP load balancing, or extreme connection handling performance. Many architectures use HAProxy for load balancing and NGINX for web serving.

Related Resources

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.