Nginx Load Balancer Setup with Private Networking
This guide walks you through setting up an Nginx load balancer that distributes traffic to backend servers over a private network. You’ll configure Nginx on a dedicated server (or VM) to proxy requests to two or more application servers, using their private IP addresses. This keeps traffic off the public internet, improving security and reducing latency.
What You’ll Need
- One server (or VM) for the load balancer, with Nginx installed (Ubuntu/Debian:
sudo apt install nginx; RHEL/CentOS:sudo yum install nginx). - Two or more backend servers running your application (web server, API, etc.), each with a private IP address.
- All servers must be on the same private network (e.g., VPC, VLAN) and able to reach each other via private IPs.
- SSH access to all servers.
- Basic familiarity with the command line and Nginx config files.
Step-by-Step
- Identify private IPs. On each backend server, run
ip addr show(orifconfig) to find the private IP (usually starting with 10., 172.16-31., or 192.168.). Note these down; you’ll need them for the Nginx config. - Test connectivity. From the load balancer, ping each backend’s private IP:
ping 10.0.0.2. If it fails, check firewall rules and network settings (security groups, NACLs) to allow traffic on the ports you’ll use (e.g., 80, 443, or your app port). - Create an Nginx config file. On the load balancer, create a new config for your site:
sudo nano /etc/nginx/sites-available/loadbalancer(or/etc/nginx/conf.d/loadbalancer.confon RHEL). - Define the upstream block. Inside the
httpblock (or directly in a server block if using a standalone file), add anupstreamblock listing your backend servers. For example:upstream backend { server 10.0.0.2:80; server 10.0.0.3:80; }This tells Nginx to treat these two servers as a group named “backend”.
- Configure the server block. Add a
serverblock that listens on the public port (e.g., 80) and proxies requests to the upstream group:server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }Replace
example.comwith your domain or IP. - Enable the config. If you used
sites-available, create a symlink:sudo ln -s /etc/nginx/sites-available/loadbalancer /etc/nginx/sites-enabled/. Then test the config:sudo nginx -t. Fix any syntax errors. - Reload Nginx. Apply the changes without dropping connections:
sudo systemctl reload nginx(orsudo service nginx reload). - Verify traffic distribution. From a client, access the load balancer’s public IP or domain. Check the backend server logs (e.g.,
/var/log/nginx/access.logon each backend) to confirm requests are being split between them. You can also enable the built-in stub_status module to see active connections. - Optional: Add health checks. Nginx open source doesn’t have active health checks, but you can use passive checks by setting
max_failsandfail_timeoutin the upstream server lines:server 10.0.0.2:80 max_fails=3 fail_timeout=30s;. This marks a server down after 3 failed attempts within 30 seconds. - Optional: Enable sticky sessions. If your app requires session persistence, use
ip_hashin the upstream block:ip_hash;before the server lines. This routes the same client IP to the same backend.
Common Mistakes
- Using public IPs in the upstream block. This defeats the purpose of private networking and may cause routing issues. Always use private IPs.
- Forgetting to open firewall ports. The load balancer must be able to reach the backend on the application port. Check firewall rules on both sides.
- Missing proxy headers. Without
X-Forwarded-For, your backend logs will show the load balancer’s IP instead of the client’s, breaking analytics and rate limiting. - Not testing the config. Skipping
nginx -tcan lead to a broken config that takes down the site when reloaded. - Using a single upstream server. A load balancer with one backend is just a proxy; you need at least two for actual load balancing.
Tips
- Use a dedicated network interface for private traffic if your provider offers one (e.g., VPC with multiple NICs).
- Set
keepalivein the upstream block (e.g.,keepalive 16;) to reuse connections to backends, reducing latency and load. - Monitor backend health with a simple script that curls each private IP and alerts if it fails.
- Use Nginx Plus if you need active health checks and more advanced features, but for most setups open source is sufficient.
- Document your IP addresses and config changes; it saves time when debugging later.
Troubleshooting
- 502 Bad Gateway. Nginx can’t reach the backend. Check: Is the backend running? Is the private IP correct? Are firewall rules allowing traffic on the port? Test with
curl 10.0.0.2:80from the load balancer. - 504 Gateway Timeout. Backend is too slow or not responding. Increase
proxy_read_timeoutin the location block, or check backend performance. - Connection refused. Backend service isn’t listening on the expected port. Check with
ss -tlnpon the backend. - Config test fails. Run
nginx -tand read the error message. Common issues: missing semicolons, braces mismatched, or upstream name conflicts. - Traffic not balanced. If you see all requests going to one server, check if you have
ip_hashenabled (it will stick to one IP), or if one server is marked down (checknginx -s reloadoutput for warnings). - Logs show load balancer IP. Ensure you’ve set the
X-Forwarded-Forheader and that your backend app is configured to read it (e.g., in Nginx backend config:set_real_ip_from 10.0.0.0/24;andreal_ip_header X-Forwarded-For;).
Frequently Asked Questions
What is the difference between round-robin and least_conn?
Round-robin (default) distributes requests sequentially. least_conn sends to the server with the fewest active connections, which is better for uneven request durations. Add least_conn; in the upstream block.
Can I use a load balancer for HTTPS traffic?
Yes. You can terminate SSL at the load balancer (using a certificate) and forward plain HTTP to backends, or pass HTTPS through with proxy_ssl_server_name and proxy_ssl_name if you have certs on each backend.
What if my backend servers are on different ports?
Just specify the port in the upstream server line, e.g., server 10.0.0.2:8080;. Make sure the proxy_pass uses the upstream name without a port.
How do I add a new backend server?
Add a new server line in the upstream block, run nginx -t, and reload Nginx. No downtime.
Is it safe to expose the load balancer to the public internet?
Yes, as long as you keep the backend servers on a private network and only allow traffic from the load balancer’s private IP. Use security groups or firewalls to restrict access.
Can I use Nginx as a load balancer for UDP traffic?
Yes, Nginx supports UDP proxying since version 1.9.13. Use stream block instead of http, and configure upstream with server lines and proxy_pass to the upstream name.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.