nginx and HAProxy Configuration

nginx upstream blocks and proxy_pass directives, HAProxy ACLs and backend pools, stats page, and connection limit tuning.

Overview

Overview nginx and HAProxy are the two dominant open-source load balancers and reverse proxies used in production infrastructure.

nginx was designed as a high-performance web server and reverse proxy. Its event-driven, asynchronous architecture handles tens of thousands of concurrent connections per worker process with low memory overhead. nginx excels at HTTP/HTTPS proxying, SSL termination, static file serving, caching, rate limiting, and gzip compression — a full layer-7 feature set in one binary.

HAProxy (High Availability Proxy) is a dedicated TCP and HTTP proxy built specifically for load balancing. It has the richest feature set for traffic management: a powerful ACL system that can inspect any field in a TCP or HTTP request, health checking with fine-grained control, connection draining, and a real-time statistics interface. HAProxy's configuration model separates concerns cleanly into frontend (listen), backend (server pool), and defaults sections.

Key differences: nginx is a web server that also does load balancing, making it a natural choice when the proxy also serves static content or needs caching. HAProxy is a pure proxy with no web server capability, making it more focused for complex routing requirements.

Both support keepalive connections to backends (critical for performance), TLS termination, and health checks. Both are used at massive scale: nginx runs on a large fraction of all web servers; HAProxy routes traffic at major social networks and financial institutions.

Key concepts

  • nginx upstream block
  • HAProxy frontend/backend split
  • HAProxy ACL
  • proxy_pass
  • keepalive (to backends)
  • HAProxy stats page

Interview questions

  • Write an nginx configuration for load balancing a Node.js API across four backends with health checks, keepalive, and rate limiting.
  • How do you configure HAProxy with ACLs to route /api/* to one backend and /static/* to another?

Sign up free to read the full topic

More Networking topics

Start free on Cariara · Coding problems · System design studio