Skip to content
EgyKode
02 · The application, in containersLab 11 / 59
Guided labdocker

Nginx Reverse Proxy & Multi-Container Docker Compose Stack

Put the whole stack behind one entry point, with Nginx serving static files and Gunicorn handling the rest.

Time
39 min
Level
Beginner
Objectives
4 objectives
Cost
Free

Where this fits in the platform

This lab adds

  • The whole application running locally, behind one entry point

The scenario#

Gunicorn is serving CSS. It is a Python process reading files off disk and writing them to a socket, and it is doing that instead of handling requests — so the site is slow under load for no good reason.

There is also no TLS, no static caching, and the application crashes on boot roughly one time in three because PostgreSQL is not accepting connections yet.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on your own machine

Run this lab on your own machine. One command starts the environment, with everything the lab needs already installed:

You will need:

  • docker
git clone https://github.com/EgyKode/EgyKode-lab.git
cd EgyKode-lab
./egykode start
./egykode shell

You need Docker and Git installed. Everything else runs inside the environment. The first start downloads it and takes a few minutes; later starts are seconds.

Not sure what you already have? Run: npm run doctor — it checks and changes nothing.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

The Nginx configuration

Step 1 of 3

What you are building#

text
  Browser
     │ :80
     v
  ┌──────────────────────── compose network ────────────────────────┐
  │  nginx        /static/  > served from a shared volume, on disk  │
  │               /         > proxy_pass to gunicorn                │
  │     │                                                           │
  │     v                                                           │
  │  web (gunicorn)  ──>  db (postgres)   ──>  named volume         │
  │                  ──>  cache (redis)                             │
  └─────────────────────────────────────────────────────────────────┘

Why not let Gunicorn serve static files? It can, and every worker that is streaming a CSS file is a worker not handling a request. Nginx does that with sendfile in the kernel, at a cost close to zero, and adds caching headers while it is there. This is the division of labour every Python deployment ends up with.


Build it#

Verify it worked#

Terminal
# Static served by nginx, not Python
curl -sI http://localhost/static/css/site.css | grep -i 'server\|cache-control'
# Server: nginx/1.27.x   Cache-Control: public, immutable
 
# Dynamic proxied through
curl -s -o /dev/null -w '%{http_code}\n' http://localhost/
 
# The app sees the real client IP
docker compose logs web --tail 5 | grep -o 'X-Forwarded-For[^ ]*'
 
# Data survives a full stop
docker compose exec db psql -U app -c "CREATE TABLE t(id int); INSERT INTO t VALUES (1);"
docker compose down && docker compose up -d
docker compose exec db psql -U app -c "SELECT * FROM t;"

That last sequence is the one people skip and the one that matters. down destroys containers and keeps named volumes; down -v destroys the volumes too, and it is one character away.


Clean up#

Terminal
docker compose down -v
docker volume prune -f

Cost of this lab: Free — everything runs locally.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 11 of 59 on the project path

Reverse Proxy & Load Balancing with NginxPut a proxy in front of two backends, then break one and watch what the health check does about it.Why next: The whole application running locally, behind one entry point50 minIntermediate

Previous: Docker Networking, Volumes & Health Checks