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
