How-to

Run an nginx Reverse Proxy in Docker

One nginx container on ports 80 and 443 can route every domain on the box to the right app container by name, so the apps never need public ports of their own.

Why put nginx in front of your containers?

A single public IPv4 has one port 443. When three apps on the same machine all want HTTPS, something has to look at the requested host name and send each request to the right place. A reverse proxy does exactly that. Running it as a container keeps the whole stack in one Compose file, makes upgrades a matter of changing an image tag, and lets the apps talk to nginx over an internal Docker network where nothing is exposed to the internet except the proxy.

What does the layout look like?

/opt/edge/
  compose.yaml
  nginx/conf.d/apps.conf
  certs/            (TLS files, read only in the container)
  www-acme/         (HTTP-01 challenge files)

Install Docker Engine and the Compose plugin from Docker's own repository on Ubuntu, Debian, Rocky or AlmaLinux. The Docker hosts page covers which of our machines suit a container host.

How do I write the Compose file?

services:
  edge:
    image: nginx:1.27-alpine
    restart: unless-stopped
    ports: ["80:80", "443:443"]
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./certs:/etc/nginx/certs:ro
      - ./www-acme:/var/www/acme:ro
    networks: [front]
  wiki:
    image: requarks/wiki:2
    restart: unless-stopped
    networks: [front]
  grafana:
    image: grafana/grafana-oss:11.3.0
    restart: unless-stopped
    networks: [front]
networks:
  front: {}

Only the proxy publishes ports. The wiki and grafana containers are reachable inside the front network by their service names, and nowhere else. Pin image tags rather than using latest, so a restart never pulls a surprise major version.

How do I route each host name to a container?

# nginx/conf.d/apps.conf
map $http_upgrade $conn_upgrade { default upgrade; '' close; }

server {
    listen 80;
    server_name wiki.example.org grafana.example.org;
    location /.well-known/acme-challenge/ { root /var/www/acme; }
    location / { return 301 https://$host$request_uri; }
}

server {
    listen 443 ssl;
    http2 on;
    server_name wiki.example.org;
    ssl_certificate     /etc/nginx/certs/wiki/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/wiki/privkey.pem;
    location / {
        proxy_pass http://wiki:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $conn_upgrade;
    }
}

Add a second 443 block for grafana.example.org pointing at http://grafana:3000. The forwarding headers let the app log the visitor's real address and know the request arrived over HTTPS; the Upgrade headers keep WebSocket features working. http2 on; is the syntax for nginx 1.25.1 and newer.

Why does nginx fail with "host not found in upstream"?

nginx resolves names in proxy_pass once at startup. If the app container is down at that moment, nginx refuses to start. Two fixes: add depends_on so the apps start first, or resolve at request time through Docker's internal DNS:

resolver 127.0.0.11 valid=10s ipv6=off;
set $wiki_up http://wiki:3000;
proxy_pass $wiki_up;

With the variable form, nginx starts even when an app is missing and picks up its new address after the app container is recreated.

Where do the proxy's TLS certificates come from?

The HTTP block above serves /.well-known/acme-challenge/ from a shared folder, so a certbot container can use the webroot method:

docker run --rm -v /opt/edge/www-acme:/var/www/acme -v /opt/edge/letsencrypt:/etc/letsencrypt \
  certbot/certbot certonly --webroot -w /var/www/acme -d wiki.example.org --agree-tos -m ops@example.org -n

Copy or symlink the files into certs/, or mount /opt/edge/letsencrypt into the proxy and point ssl_certificate at the live path. Run the same container with renew from a systemd timer, then reload nginx. A host-level certbot setup is described in Let's Encrypt with nginx.

How do I reload after a config change?

docker compose exec edge nginx -t
docker compose exec edge nginx -s reload

A reload keeps existing connections alive, unlike restarting the container. Keep the conf directory under git so every change is easy to review and roll back.

Anything to watch on a bare metal host?

Docker publishes ports by inserting its own iptables rules, which bypass ufw's INPUT rules. Because only the proxy publishes ports here, that is exactly what you want; just never publish an app or database port by mistake. Bind admin-only ports to the loopback address instead, for example "127.0.0.1:9090:9090". For dozens of sites, a box with more threads such as S11 or D1 gives room for both the proxy and the apps. See iptables port forwarding for the NAT concepts Docker uses underneath.

Machines Sized for a Container Host

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
S3Intel Xeon E-2356G6 / 1264 GB DDR42x 1 TB NVMe2x 10 Gbps$80.38$80.38 (1 month)
S11Intel Core i9-14900K24 / 32128 GB DDR52x 2 TB NVMe2x 10 Gbps$149.39$149.39 (1 month)
D12x Intel Xeon Gold 5315Y16 / 32128 GB DDR42x 1 TB NVMe2x 25 Gbps$202.94$202.94 (1 month)
D62x Intel Xeon Gold 531724 / 48256 GB DDR42x 2 TB NVMe2x 25 Gbps$325.96$325.96 (1 month)

Keep Reading

Docker hosts

One box, many Compose stacks: a reverse proxy on ports 80 and 443 routes every container by hostname.

Open

Let's Encrypt with nginx

Get free TLS certificates for nginx with certbot, choose the nginx or webroot method, and confirm automatic renewal works.

Open

iptables port forwarding

Forward a public port to a VM, container or remote host with DNAT, enable routing, and keep replies flowing back the right way.

Open

SaaS hosting

Host a multi-tenant web app on bare metal: app, workers and database split by box as customers grow.

Open

FAQ

Quick Answers

No. They join the same Docker network as the proxy, which reaches them by service name. Only the proxy publishes 80 and 443.

Pass X-Real-IP and X-Forwarded-For from nginx, and configure the app to trust the proxy network as a forwarding source.

It resolves upstream names at startup. Use depends_on, or a resolver line with a variable in proxy_pass.

Published ports get Docker iptables rules that skip ufw input rules. Publish only the proxy, and bind internal tools to 127.0.0.1.

Reload with nginx -s reload inside the container. It keeps connections open and applies the new config.