Nginx · The edge of your stack

What is an upstream block and how do you tune it?

Mediumupstreamkeepalivebackend poolUpdated Oct 2026
Quiz this track

Short answer

An upstream block names a pool of backend servers and configures balancing, weights, failure handling and keepalive. Reusing proxy_pass against that name centralizes backend behavior.

Instead of repeating backend addresses in many locations, define them once in an upstream. The block also holds per-server parameters such as weight, max_fails, fail_timeout, backup and down.

nginx
upstream app {
    least_conn;

    server app-1.internal:8080 weight=2 max_fails=3 fail_timeout=10s;
    server app-2.internal:8080;
    server app-3.internal:8080 backup;

    keepalive 64;
}

server {
    location / {
        proxy_pass http://app;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}
The empty Connection header is required for upstream keepalive to work with HTTP/1.1.

Parameters that matter

  • weight sends proportionally more traffic to larger instances
  • max_fails and fail_timeout control passive ejection after failures
  • backup servers receive traffic only when primary servers are unavailable
  • down administratively removes a server without editing the upstream method
  • keepalive caches idle upstream connections per worker
Hostnames in upstream server lines are normally resolved when the config loads. If backend IPs change frequently, use a resolver with the resolve parameter on versions that support it, or regenerate the upstream and reload.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why is upstream keepalive important for latency?
  2. 2What happens when a hostname in an upstream changes IP?
  3. 3How do max_fails and fail_timeout interact?
  4. 4When would you use a Unix socket instead of a TCP upstream?

Say it in the interview

An upstream block defines a named backend pool and centralizes balancing, weights and failure settings. I add keepalive and then set proxy_http_version 1.1 with an empty Connection header so Nginx reuses backend connections. I also account for DNS: by default names resolve at startup, so rotating backends need a resolver-aware setup or a config reload.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Nginx mock interview and get scored feedback on depth, clarity and confidence.