What are upstream servers in Nginx and how do you configure them?
Learn what Nginx upstream servers are, how to configure the upstream block with proxy_pass, weights, and failover for scalable load balancing.
Expected Interview Answer
Upstream servers in Nginx are a named group of backend application servers, defined with the upstream block, that Nginx forwards requests to and load-balances across when acting as a reverse proxy.
You declare the group with upstream name { server ... } in the http context, then reference it in a location using proxy_pass http://name. Nginx distributes incoming requests among the listed servers using a balancing method (round-robin by default, or least_conn, ip_hash, hash, random), and you tune each member with parameters like weight, max_fails, fail_timeout, backup, and down. This lets a single Nginx front-end spread traffic, survive individual backend failures, and scale horizontally.
- Distributes load across multiple backends
- Provides high availability with automatic failover
- Enables horizontal scaling behind one endpoint
- Supports weighted and session-sticky routing
- Health tracking via max_fails and fail_timeout
AI Mentor Explanation
Think of upstream servers as a bowling attack a captain rotates through. Instead of forcing one bowler to bowl every over until they tire and leak runs, the captain shares overs across several bowlers by strength — the fast bowler gets a higher weight of overs, the spinner covers others, and an injured bowler is marked unavailable so nobody sends them the ball.
Step-by-Step Explanation
Step 1
Declare the upstream block
In the http context, add upstream backend { server 10.0.0.1:8080; server 10.0.0.2:8080; } listing each backend.
Step 2
Reference it in proxy_pass
Inside a location block, use proxy_pass http://backend; so Nginx forwards matched requests to the group.
Step 3
Pick a balancing method
Keep round-robin, or add least_conn, ip_hash, or hash $request_uri for the routing behaviour you need.
Step 4
Tune per-server parameters
Set weight, max_fails, fail_timeout, backup, and down on each server line to control share and failover.
Step 5
Set proxy headers and reload
Forward Host and X-Forwarded-For, then run nginx -t and nginx -s reload to apply without downtime.
What Interviewer Expects
- Knows the upstream block lives in the http context
- Can wire it up with proxy_pass
- Names balancing methods beyond round-robin
- Explains weight, max_fails, fail_timeout, backup
- Understands failover and horizontal scaling
Common Mistakes
- Putting proxy_pass to a single IP instead of a named upstream
- Forgetting to forward Host and X-Forwarded-For headers
- Confusing weight with priority or thinking backup servers get normal traffic
- Assuming ip_hash guarantees sessions survive backend removal
- Not reloading or testing config with nginx -t after changes
Best Answer (HR Friendly)
“Upstream servers are a group of backend machines that Nginx spreads incoming web traffic across. It means one website address can be served by several servers, so the site stays fast and keeps working even if one server goes down.”
Code Example
upstream app_backend {
least_conn;
server 10.0.0.1:8080 weight=3 max_fails=2 fail_timeout=30s;
server 10.0.0.2:8080;
server 10.0.0.3:8080 backup;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Follow-up Questions
- What load-balancing methods does Nginx support and when would you use each?
- How do max_fails and fail_timeout provide passive health checks?
- What does a backup server do in an upstream block?
- How does ip_hash achieve session persistence?
- How do active health checks differ, and where are they available?
MCQ Practice
1. Which directive references an upstream group from a location block?
proxy_pass http://groupname; routes matched requests to the named upstream block.
2. What is the default load-balancing method in an Nginx upstream block?
Nginx distributes requests in round-robin order by default unless another method is specified.
3. What does the backup parameter on an upstream server do?
A backup server stays idle and only receives requests when the non-backup servers are down.
Flash Cards
Where is the upstream block defined? — In the http context, then referenced via proxy_pass in a location or server block.
Default balancing method? — Round-robin.
What does weight=3 mean? — That server receives roughly three times the requests of a weight=1 server.
Passive health-check parameters? — max_fails and fail_timeout mark a server temporarily unavailable after repeated failures.
Method for session stickiness by client IP? — ip_hash.