Load balancing distributes incoming traffic across multiple servers or application instances that can handle it. NGINX acts as a reverse proxy: it receives a request, selects a configured destination, and forwards the request there. This can scale containerized or non-containerized applications without immediately requiring a larger orchestration system. section 3.6 section 7.5
NGINX can rotate requests among destinations using several load-balancing algorithms:
- Round Robin sends requests one after another and is the default. section 7.4.1
- Weighted Round Robin prefers some destinations over others. section 7.4.1
- Least Connections selects the least busy destination. section 7.4.1
- IP Hash selects destinations according to the requesting IP address. section 7.4.1
Balance several application instances
Define an NGINX upstream named myapp with destinations such as localhost:8080, localhost:8081, and servers on other IP addresses. Proxy requests for / to http://myapp. In Docker Compose, run several copies of the same application under service names such as bruce, tony, and thor, then configure the upstream with bruce:8080, tony:8080, and thor:8080. Publish NGINX on port 8000 while the application services remain behind it. section 7.4.1
Listing 7.16 NGINX load balancing basics
upstream myapp {
server localhost:8080;
server localhost:8081;
server my-ip-address:80;
server my-other-ip-address:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://myapp;
}
location /api {
proxy_pass http://localhost:3000;
}
}
Shows an NGINX upstream containing several local and remote destinations, with a separate route for /api. section 7.4.1












