
When traffic grows beyond what a single server can handle, a load balancer becomes essential. Nginx includes production-grade load balancing built in — no extra software required. This guide walks through configuring Nginx as a load balancer on VPS Ubuntu to distribute requests across multiple backend servers.
How Load Balancing Works
A load balancer sits in front of backend servers, receiving requests from clients and forwarding them to the most suitable backend. Benefits include handling more traffic, eliminating single points of failure, and enabling zero-downtime deployments.
Round-Robin
Rotates requests evenly across all servers. Best when backends have identical specs.
Least Connection
Sends each new request to the server with the fewest active connections. Best for long-running requests.
IP Hash
Same client IP always routes to the same backend. Best for apps requiring sticky sessions.
Architecture Overview
In this example, a VPS running as the Load Balancer (IP: 10.0.0.1) accepts traffic on ports 80/443 and distributes it to two backend servers (10.0.0.2 and 10.0.0.3) running your application on port 8080.
Install Nginx on the Load Balancer VPS
Configure Upstream and Load Balancing
Create a new configuration file:
Add this configuration (Round-Robin):
Switching to Least Connection
Switching to IP Hash (Sticky Sessions)
Weighted Distribution for Different Server Specs
Enable Configuration and Test
Tuning Timeouts and Buffers for Production
Properly tuning proxy timeouts and buffer settings prevents failed requests when backends are slow or responses are large. Add these directives inside the location / block or at the server level.
For low-latency API endpoints that return small responses, you can disable buffering with proxy_buffering off; — but this increases load on Nginx worker processes, so benchmark before applying globally.
SSL/TLS Termination on the Load Balancer
The most common production approach is SSL Termination: the load balancer handles HTTPS from clients and forwards plain HTTP internally to backend servers. Backends no longer need certificates, reducing overhead and simplifying certificate management.
Rate Limiting to Protect Against Traffic Spikes
Because the load balancer is the single entry point for all traffic, it is the ideal place to enforce rate limiting. This protects backend servers from traffic spikes, brute-force attacks, and excessive scraping without touching application code.
| Rate | Recommended For | Notes |
|---|---|---|
| 10r/s | General API | Moderate restriction |
| 1r/s | Login endpoint | Strict — prevents brute-force |
| 100r/s | Static assets | Loose — public resources |
Passive Health Checks
Nginx Open Source supports passive health checks automatically — if a backend consistently returns errors, Nginx temporarily removes it from the pool:
Note: Active Health Checks (probing backends every N seconds) require Nginx Plus (commercial) or Nginx + Lua. For open-source active health checking, consider HAProxy as an alternative.
Next Steps After Your Load Balancer Is Running
Once Nginx is distributing traffic successfully, a few more steps will make your setup production-ready and maintainable long-term.
- Enable SSL/TLS: Use Let's Encrypt and Certbot to obtain a free certificate for the load balancer and redirect all HTTP traffic to HTTPS automatically.
- Set up monitoring: Use Uptime Kuma, Prometheus, or a similar tool to track uptime and latency for both the load balancer and each individual backend. Set alerts for error rate spikes and backend failures.
- Configure log rotation: Prevent access logs from filling the disk by setting up logrotate to rotate daily and retain 30 days of history.
- Test failover regularly: Simulate backend failure by stopping one server and confirming that traffic continues flowing through the remaining backends. Run this test after every config change.
- Plan rolling deployments: Use a script or tool like Ansible to deploy updates to backends one at a time while the load balancer keeps the others in rotation — this achieves zero-downtime deploys without extra infrastructure.
Nginx load balancing on a VPS is one of the most cost-effective ways to build a high-availability architecture. The software is open source with no licensing cost, and it can handle very high request volumes on modest hardware. AsiaGB VPS with full root access lets you configure Nginx exactly to your requirements with no restrictions from the hosting provider.
Monitor the Load Balancer
VPS for High-Traffic Applications at AsiaGB
AsiaGB VPS runs Linux Ubuntu/Debian with full root access — configure Nginx load balancing exactly as needed. Plans from 500 THB/month.
View VPS Plans