An Nginx load balancer config builder that turns a list of backend servers into a valid
upstream block plus a server block that proxies to it. Choose a balancing strategy, tune
passive health checks per server, and copy a config that includes the standard forwarding
headers so your apps see the real client IP and scheme.
How it works
Nginx load-balances by defining an upstream group of server directives and pointing a
proxy_pass at it by name. The builder emits each backend as
server host:port weight=… max_fails=… fail_timeout=…, optionally tagged backup or down.
The balancing method line (least_conn; or ip_hash;) is placed first inside the block when
you pick a non-default strategy.
The server block listens on your chosen port and proxies every request to the upstream over
HTTP/1.1 with an empty Connection header — required for keepalive connection reuse. It
sets Host, X-Real-IP, X-Forwarded-For, and X-Forwarded-Proto so the backend can
reconstruct the original request, and uses proxy_next_upstream to retry the next server on
errors or 502/503 responses.
Tips and notes
- Use weights to send proportionally more traffic to larger machines, for example
weight=5versusweight=1for a smaller node — this is ignored underip_hash. - Passive health checking via
max_fails/fail_timeoutis built in, but it only reacts to real traffic. For active probing you need Nginx Plus or an external health checker. - Always run
nginx -tbefore reloading. A typo in anupstreamserver line fails the test and areloadwill refuse to apply a broken config. - Set a
backupserver for graceful failover capacity that stays idle until every primary is down, rather than sharing live load.