liveBook · Manning

NGINX

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

NGINX is web server and reverse-proxy software distributed as a container image with multiple version and feature variants.

When several containerized applications need to share ports 80 and 443, NGINX can be their single public entry point. It receives HTTP requests, selects an application container, and returns its response. The application containers need no published ports. NGINX can also serve content itself. section 20.0 section 20.1

To put an application behind NGINX:

  • Create a Docker network and start NGINX with a bind-mounted site-configuration folder. Its default page lets you check that the server is running before adding a proxy. section 20.1
  • Start the application on the same network without publishing its port. Map a local domain to your machine in the hosts file, then add an NGINX site configuration for that domain. section 20.1
  • Reload NGINX and request the domain with curl. Start the application before reloading: if a configured upstream is unavailable when NGINX reads the configuration, NGINX exits. 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
  }
}

A site configuration matches the request’s hostname, proxies its path to an application container, forwards the Host header, and adds a response header identifying the proxy. section 20.1

Routing can use either a hostname or a request path, letting one domain expose a web application and an API. With multiple upstream containers, NGINX can load balance requests without changes to application code. An HTTP site can also redirect to HTTPS, with NGINX serving the certificate. section 20.2

Check whether caching helps

Configure NGINX to cache the image gallery’s web and API responses, then make the same request twice. An X-Cache: MISS first shows that NGINX fetched the response upstream; a subsequent HIT shows it served the cached copy. The API example keeps successful responses for one minute, while the web configuration uses a longer cache and can serve stale content during specified upstream failures. Cache only content suitable for sharing between users. section 20.3

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

How deep it goes
Works through site configuration, reload order, routing, HTTPS, load balancing, and caching; does not descend into server internals.
Examples
Tests a local who-am-I site and an image gallery with curl, then checks cache hits through response headers.
What it assumes
Assumes basic container and command-line use while walking through Docker network creation and NGINX site configuration.
What you can do after
Operate/configure it to expose internal containers, route requests, enable HTTPS, and cache suitable responses.
How it connects
Connects NGINX to reverse proxies, Docker networks, HTTP Host headers, load balancing, TLS certificates, and HTTP caching.

Also explained in

Compare how 3 books explain NGINX →

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.