Two popular load balancers and reverse proxies, and how you configure them.
Nginx and HAProxy are two very popular, free programs that sit in front of your servers. Both can work as a reverse proxy and as a load balancer.
A reverse proxy accepts requests from users and forwards them to servers behind it. A load balancer adds the rule for which server gets each request. Nginx and HAProxy do both. You install one on a machine, point DNS at it, and list your app servers in its config.
upstream backend {
least_conn; # default is round robin
server 10.0.0.1:8080 weight=3;
server 10.0.0.2:8080;
server 10.0.0.3:8080 backup; # used only if the others fail
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
}
}frontend web
bind *:80
mode http
default_backend app
backend app
mode http
balance leastconn # roundrobin, leastconn, source ...
option httpchk GET /health # active health check
server app1 10.0.0.1:8080 weight 3 check
server app2 10.0.0.2:8080 check
server app3 10.0.0.3:8080 check backup| Topic | Nginx | HAProxy |
|---|---|---|
| Main identity | Web server that is also a reverse proxy and balancer | Dedicated load balancer and proxy |
| Config shape | Blocks: http, upstream, server, location | Sections: global, defaults, frontend, backend |
| Serves static files | Yes, very commonly used for it | Not its purpose |
| Algorithms (common) | Round robin (default), least_conn, ip_hash, hash | roundrobin, leastconn, source, uri, and others |
| Health checks | Passive checks in the open-source version, active checks in commercial or extra modules | Rich active checks built in, plus fine control of when to remove a server |
| TCP / UDP balancing | Yes, through the stream module | Yes, a core use (TCP mode) |
| Statistics | Basic status page, metrics through add-ons | Detailed built-in stats page and runtime API |
| Typical choice | One tool for web serving, caching and proxying | A balancer in front of many services, with strong checks and TCP |
upstream backend {
server 10.0.0.1:8080;
server 10.0.0.2:8080;
server 10.0.0.3:8080;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}A teaching model: the request list, durations and client hashes are fixed, and it uses a smooth weighted round robin. Real tools differ in tie-breaking and in details that change between versions, so check the official docs. The addresses are examples.
nginx -t # test the Nginx config haproxy -c -f /etc/haproxy/haproxy.cfg # check the HAProxy config
| Term | Meaning in easy words |
|---|---|
| Reverse proxy | A server that receives requests for other servers and forwards them |
| Load balancer | A reverse proxy that also decides which backend server gets each request |
| Upstream (Nginx) | A named group of backend servers |
| Backend (HAProxy) | A named group of servers that a frontend can send traffic to |
| Frontend (HAProxy) | The listening side: which address and port to accept traffic on |
| proxy_pass | The Nginx directive that forwards a request to an upstream or URL |
| Weight | A number that makes a server receive more or less than its share |
| Health check | A test the proxy runs to see if a server is fit to receive traffic |
| TLS termination | Ending the encrypted HTTPS connection at the proxy |
| Graceful reload | Applying a new config without dropping current connections |
| Stick table / ip_hash | Ways to keep a client on the same backend |
| Choice | We gain | We lose |
|---|---|---|
| Self-managed Nginx or HAProxy | Full control, no vendor lock-in, often cheaper | You operate it: upgrades, scaling, high availability |
| Cloud managed balancer | Less to run, built-in scaling and availability | Less control, cost, tied to the provider |
| Nginx | One tool for web serving, caching and proxying | Fewer built-in balancer features in the open-source build |
| HAProxy | Strong balancing, checks and statistics, good for TCP | Not a web server, no file serving |
| Layer 7 mode | Route by URL, header or cookie | More CPU, must parse requests |
| Layer 4 mode | Very fast, protocol agnostic | Cannot look inside the request |