DEV Community

Arnob
Arnob

Posted on

What is HAProxy?

HAProxy is a high-performance open-source load balancer and reverse proxy server commonly used to distribute web traffic across multiple backend servers

captionless image

HAProxy (High Availability Proxy) is a powerful, open-source load balancer and reverse proxy designed to deliver high performance, reliability, and scalability for modern applications. It sits between clients and backend servers, intelligently distributing incoming traffic to ensure applications remain fast, resilient, and highly available even under heavy load.

Originally built to solve large-scale traffic challenges, HAProxy has become a trusted backbone for many high-traffic platforms worldwide. It supports both Layer 4 (TCP) and Layer 7 (HTTP/HTTPS) traffic, making it suitable for web applications, APIs, microservices, and even database connections. With built-in health checks, advanced routing logic, and extremely low latency, HAProxy excels at keeping services online while seamlessly handling failures behind the scenes.

Today, HAProxy is widely used in cloud, containerized, and on-premise environments, often acting as the first point of contact for incoming traffic. Its proven stability, unmatched performance, and fine-grained control make it a preferred choice for engineers who need a reliable traffic management solution at scale.

In simple terms, HAProxy sits in front of your applications and helps:

  • Spread traffic evenly across servers
  • Improve reliability and uptime
  • Handle failover if a server crashes
  • Speed up applications
  • Terminate SSL/TLS (HTTPS)
  • Protect against overload

Typical architecture

Users
  ↓
HAProxy
  ↓
App Server 1
App Server 2
App Server 3
Enter fullscreen mode Exit fullscreen mode

If one app server goes down, HAProxy automatically routes traffic to healthy servers.

Is HAProxy an alternative to nginx?

Yes — HAProxy can be an alternative to NGINX in some cases, but they are designed with slightly different priorities.

Difference

| Feature             | HAProxy                  | NGINX               |
| ------------------- | ------------------------ | ------------------- |
| Primary role        | Load balancer & proxy    | Web server + proxy  |
| Best at             | Traffic distribution, HA | Serving web content |
| Static file hosting | No                       | Yes                 |
| Load balancing      | Excellent                | Very good           |
| TCP/Layer 4 support | Excellent                | Good                |
| HTTP reverse proxy  | Excellent                | Excellent           |
| Performance         | Extremely high           | Extremely high      |
| Kubernetes ingress  | Common                   | Very common         |
Enter fullscreen mode Exit fullscreen mode

Simple Explanation

Think of them like this:

  • HAProxyTraffic manager
  • NGINXWeb server + traffic manager

So:

  • If you need only load balancing → HAProxy can replace NGINX
  • If you need to serve websites/files → HAProxy cannot replace NGINX

When is HAProxy better

Use HAProxy when you need:

  • High availability (HA)
  • Advanced load balancing
  • TCP/UDP proxying
  • Massive concurrent connections
  • Database load balancing
  • Health checks and failover
  • Low-latency routing

Example:

  • PostgreSQL cluster
  • API gateway
  • Kubernetes ingress
  • Multi-server backend systems

When NGINX is better

Use NGINX when you need:

  • A web server
  • Static file serving
  • PHP hosting
  • CDN-like caching
  • Simple reverse proxy
  • SSL termination with web hosting

Example:

  • WordPress hosting
  • Laravel hosting
  • Static websites
  • React frontend serving

Common real-world setup

Many production systems use both together:

Internet
   ↓
HAProxy
   ↓
NGINX
   ↓
Application Servers
Enter fullscreen mode Exit fullscreen mode

Why?

  • HAProxy handles traffic balancing and failover
  • NGINX serves web content and handles caching

HAProxy vs NGINX for load balancing

Both can load balance traffic, but HAProxy is usually stronger for:

  • Advanced routing rules
  • Connection handling
  • Health checks
  • Session persistence
  • HA clustering

NGINX is often easier when you’re already using it as a web server.

Example comparison

HAProxy

backend app
    balance roundrobin
    server app1 10.0.0.1:80 check
    server app2 10.0.0.2:80 check
Enter fullscreen mode Exit fullscreen mode

NGINX

upstream app {
    server 10.0.0.1:80;
    server 10.0.0.2:80;
}
server {
    location / {
        proxy_pass http://app;
    }
}
Enter fullscreen mode Exit fullscreen mode

When HAProxy IS a Good Alternative to NGINX

Use HAProxy instead of NGINX if:

  • You don’t need to serve static files
  • You want extreme performance
  • You’re balancing:
    • APIs
    • Microservices
    • Databases
    • Kubernetes services
  • You need advanced TCP routing

Example:

Internet → HAProxy → App Servers
Enter fullscreen mode Exit fullscreen mode

When HAProxy is NOT an Alternative

HAProxy is not a replacement if you need:

  • Static website hosting
  • PHP / FastCGI handling
  • Caching web content
  • Full web server features

Example:

Internet → NGINX → PHP-FPM
Enter fullscreen mode Exit fullscreen mode

Best-Practice Setup

captionless image

Internet
   ↓
HAProxy  (Load Balancer)
   ↓
NGINX    (Web Server)
   ↓
App / PHP / Node / API
Enter fullscreen mode Exit fullscreen mode

Why this combo works best:

  • HAProxy handles traffic, SSL, and failover
  • NGINX handles static files, caching
  • Best performance + flexibility

HAProxy + NGINX config

Production-style configuration showing HAProxy in front of NGINX. This setup is very common in real infrastructure.

Architecture (HAProxy → NGINX)

From Chatgpt

Internet (HTTPS)
      ↓
HAProxy (SSL, Load Balancer)
      ↓
NGINX (Web Server / Reverse Proxy)
      ↓
App (PHP / Node / API)
Enter fullscreen mode Exit fullscreen mode

Scenario Assumptions

  • HAProxy server: 10.0.0.1
  • NGINX servers: 10.0.0.10 10.0.0.11
  • Domain: example.com
  • SSL terminates at HAProxy
  • NGINX serves HTTP only

HAProxy Configuration (Frontend + Backend)

📄 /etc/haproxy/haproxy.cfg

global
    log /dev/log local0
    maxconn 50000
    daemon
    ssl-default-bind-options no-sslv3
    ssl-default-bind-ciphers PROFILE=SYSTEM
defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5s
    timeout client  50s
    timeout server  50s
# HTTP → HTTPS redirect
frontend http_front
    bind *:80
    redirect scheme https if !{ ssl_fc }
# HTTPS frontend
frontend https_front
    bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
    default_backend nginx_backend
# Backend NGINX servers
backend nginx_backend
    balance roundrobin
    option httpchk GET /
    http-check expect status 200
    server nginx1 10.0.0.10:80 check
    server nginx2 10.0.0.11:80 check
Enter fullscreen mode Exit fullscreen mode

HAProxy responsibilities:

  • SSL termination
  • Load balancing
  • Health checks
  • HTTP → HTTPS redirect

NGINX Configuration (Backend Web Server)

📄 /etc/nginx/conf.d/app.conf

server {
    listen 80;
    server_name example.com;

    root /var/www/html;
    index index.html index.php;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
}

Enter fullscreen mode Exit fullscreen mode

NGINX responsibilities:

  • Serve static files
  • Handle PHP / app logic
  • App-level routing
  • Logging & caching

Enable Real Client IP (Important)

Without this, NGINX will see HAProxy’s IP only.

HAProxy: forward client IP

frontend https_front
    option forwardfor
Enter fullscreen mode Exit fullscreen mode

NGINX: trust HAProxy IP

set_real_ip_from 10.0.0.1;
real_ip_header X-Forwarded-For;
Enter fullscreen mode Exit fullscreen mode

Test & Reload Services

HAProxy

sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy
Enter fullscreen mode Exit fullscreen mode

NGINX

sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Where This Is Used

  • High-traffic websites
  • E-commerce platforms
  • API gateways
  • PHP/Laravel/WordPress stacks
  • Microservices frontends

Why This Setup Is Used in Production

| Task            | HAProxy | NGINX |
| --------------- | ------- | ----- |
| SSL termination | ✅       | ❌    |
| Load balancing  | ✅       | ⚠️    |
| Health checks   | ✅       | ❌    |
| Static files    | ❌       | ✅    |
| App handling    | ❌       | ✅    |
| Extreme traffic | ✅       | ⚠️    |
Enter fullscreen mode Exit fullscreen mode

Top comments (0)