Nginx Upstream Groups and Weighted Round Robin
One public endpoint, several application instances
A reverse proxy can forward requests to a pool rather than one fixed destination. An upstream block in the HTTP context gives that pool a name. Each member is a peer. The public route refers to the group by name, leaving Nginx to choose an eligible peer. The application instances must implement the same response contract. In this chapter, the two lab backends return distinct synthetic labels so that you can see which one answered.
upstream catalog_pool { server 127.0.0.1:18171; server 127.0.0.1:18172;}server { listen 127.0.0.1:18170; location / { proxy_pass http://catalog_pool; }}The browser connects to port 18170, not directly to either backend. Both members can receive /products and return a response. These fragments are alternatives, not a single combined config; the complete lab is in lab-fixtures/chapter-16/.
Default and weighted selection
Without another balancing method, Nginx uses weighted round robin. Equal weights favor an even share over ordinary healthy requests. Increasing a peer weight increases its intended relative share; it does not reserve CPU, replicate data or guarantee a particular request ordering.
upstream catalog_weighted { server 127.0.0.1:18171 weight=2; server 127.0.0.1:18172 weight=1;}For 30 comparable requests in a simplified healthy model, A would receive about 20 and B about 10. A real sample can differ because eligibility, concurrent work, retries and worker state matter. Measure actual response markers instead of asserting an exact sequence.
client -> Nginx -> A or BGET /products -> 200 {"peer":"A"}GET /products -> 200 {"peer":"B"}Observe the selection without exposing internal infrastructure
log_format balance 'status=$status peer=$upstream_addr upstream=$upstream_status';access_log logs/balance.log balance;The upstream address and status may list multiple attempts, so do not equate every incoming request with exactly one backend operation. Keep diagnostic logs private. The lab disables access logging and exposes only fictional peer markers. A response marker indicates who answered; it does not prove that the pool has spare capacity. Repeated client requests on one HTTP connection are not necessarily pinned to one backend. Long-lived proxied streams can remain attached to their selected peer for their lifetime.
Choose the simplest design first
Round robin is a useful starting point for similar stateless application instances with comparable request durations. If one server is twice as capable, try a reviewed weight and measure latency and saturation rather than using hardware ratios blindly. Use an external shared or replicated session store where the application needs it. A load balancer does not synchronize in-memory shopping carts or make a database highly available.
Practice
A, B and C have weights 3, 2 and 1. Estimate their shares for 60 comparable requests; then describe one reason measured shares may differ and one way to observe them.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Upstream Groups and Weighted Round Robin
Apply upstream selection concepts to original examples.
Where does the HTTP upstream block belong?
How does the default method distribute work?
What share is intended for weight 2 versus weight 1?
What does an A response marker prove?
Does one client HTTP connection guarantee one upstream peer?