liveBook · Manning

Upstream service

As explained in Learn Docker in a Month of Lunches, Second Edition

An upstream service is an application component that receives forwarded requests from a reverse proxy and generates their responses.

An upstream service is the application behind a reverse proxy. A client sends a request to the proxy; the proxy forwards it to an upstream container, then returns that container’s response to the client. In a simple pass-through setup, even an application failure reaches the client unchanged. section 20.1

To connect nginx to an upstream container, configure the public hostname and use the container’s name and port as the proxy destination. Start the application before reloading nginx: nginx checks that its configured upstreams are available when it reads the configuration and exits if one is unavailable. section 20.1 section 20.2

Listing 20.1 Nginx proxy configuration for the who-am-I website
server {
  server_name whoami.local;               # the domain hostname

  location / {
    proxy_pass        http:/./whoami:8080; # source address for content
    proxy_set_header  Host $host;         # copy the host header
    add_header        X-Host $hostname;   # add proxy name in response
  }
}

This nginx site configuration routes requests for whoami.local to the whoami application container on port 8080. section 20.1

Once the route works, the proxy can handle concerns that would otherwise fall to the application:

  • Route by hostname or request path, so a website and its API can use one public domain while reaching different upstream containers. section 20.2
  • Load balance across multiple containers. Repeated requests to the image gallery show different container IP addresses in the X-Upstream response header. section 20.2
  • Cache suitable responses. An X-Cache: MISS indicates a fetch from the upstream; a subsequent HIT can be served without contacting it. Avoid caching user-specific content. section 20.3

Traefik calls these upstream components services. Its routers match incoming requests to a service, while middleware can alter a request before it reaches that service. Container labels can provide the routing rule and upstream port. section 20.4 section 20.4

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains pass-through responses, routing, availability checks, load balancing, and caching through container configurations rather than application internals.
Examples
Uses a who-am-I website, an image gallery with multiple web containers, and an API routed by request path.
What it assumes
Assumes a working container environment while introducing reverse-proxy and upstream roles through runnable Docker Compose examples.
What you can do after
Operate/configure it by starting application containers, configuring nginx or Traefik routing, and checking forwarded requests.
How it connects
Connects reverse proxies to container DNS, hostname and path routing, load balancing, and proxy caching.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.