Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 75/ 160
04. Docker Networking

Reverse Proxying Containers with Nginx & Caddy

Front containers with Nginx or Caddy for TLS termination and path-based routing.

IntermediateNginx · Caddy3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Proxy a small public port or two and leave app containers unpublished, while preserving forwarding, WebSocket and timeout headers.

Think of it like…

The proxy is the building concierge: it accepts TLS at the front desk and routes visitors by appointment to internal room names.

Where you’ve seen it

In practice, Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2 is the concrete move for this job: front containers with Nginx or Caddy for TLS termination and path-based routing. The release is not accepted until curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto confirms the result.

Words you’ll hear

Nginx
The implementation control in Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2 for front containers with Nginx or Caddy for TLS termination and path-based routing.
Caddy
The result to observe with curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto, rather than infer from a successful command alone.
Reverse Proxy
The production boundary exposed when publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.

Visual diagram

Loading interactive diagram

Theory & deep dive

Nginx control path

The concrete mechanism is Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2. The observable result is curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto; if those disagree, inspect the runtime evidence before changing unrelated settings.

Front containers with Nginx or Caddy for TLS termination and path-based routing. Keep Nginx, Caddy and Reverse Proxy in the same deployment record so the intent and the observed state cannot drift apart.

Concrete Nginx setup

Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2

Key takeaway: Prove Caddy with curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto, not with command success alone.

Reverse Proxy failure boundary

A proxy pointed at localhost looks for itself, not the service container. Lost X-Forwarded-Proto, Host or upgrade headers breaks redirects and WebSockets, and buffering/upload defaults can truncate large APIs. The narrow failure to design for is publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.

Accept this compromise explicitly: Nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle. The controlled check-curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.

Key takeaway: Nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A proxy pointed at localhost looks for itself, not the service container. Lost X-Forwarded-Proto, Host or upgrade headers breaks redirects and WebSockets, and buffering/upload defaults can truncate large APIs.

Proxy a small public port or two and leave app containers unpublished, while preserving forwarding, WebSocket and timeout headers.
Key numbers at this step
Intent
Nginx

When to use it & trade-offs

What it is

Nginx or Caddy terminates client connections, applies host/path routing and forwards to container names on a shared network. Nginx offers explicit configuration and mature modules; Caddy can reduce certificate and HTTPS configuration through automatic ACME.

Why it matters: A proxy pointed at localhost looks for itself, not the service container. Lost X-Forwarded-Proto, Host or upgrade headers breaks redirects and WebSockets, and buffering/upload defaults can truncate large APIs.

When to reach for it

  • The fit is strongest for this job: front containers with Nginx or Caddy for TLS termination and path-based routing. Pair it with curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto in the runbook.
  • Production Nginx changes should make Caddy observable through Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2, rather than trusting the runtime default.
  • Keep a rollback example for the case where publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.

When to avoid or be careful

  • Do not accept command success while the system can still fail by publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.
  • Avoid erasing Reverse Proxy protection merely to dodge this cost: “Nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle”.
  • Do not replace the evidence path-curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto-with a screenshot or a mutable tag.

Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2: practical choices

Explicit Nginx
Best for: Front containers with Nginx or Caddy for TLS termination and path-based routing, with a reviewed runbook.

Strengths: Nginx or Caddy terminates client connections, applies host/path routing and forwards to container names on a shared network. Nginx offers explicit configuration and mature modules; Caddy can reduce certificate and HTTPS configuration through automatic ACME.

Watch out: Nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle.

Ambient default
Best for: Short-lived Caddy experiments with low blast radius.

Strengths: It can begin with Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2 and fewer surrounding controls.

Watch out: It silently accepts publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol, which makes the result hard to reproduce after promotion.

Managed Reverse Proxy
Best for: Teams that centralize Reverse Proxy policy across many workloads.

Strengths: A proxy pointed at localhost looks for itself, not the service container. Lost X-Forwarded-Proto, Host or upgrade headers breaks redirects and WebSockets, and buffering/upload defaults can truncate large APIs.

Watch out: The platform adds an ownership and debugging layer; retain curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto as the direct check.

Interview signals & numbers to know

When this topic comes up

  • Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2
  • curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto
  • Failure signature: publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol
  • Trade-off: Nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle.

Benchmarks & rules of thumb

443/tcp -
normal public HTTPS listener
0 backend host ports -
need to be published when the proxy shares its network
101 -
HTTP status used for a successful WebSocket upgrade

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2 as proof and skipping curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto.
  • Publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol
  • Optimizing Nginx without accepting that nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle.

Likely interviewer follow-ups

  • Why does Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2 address Nginx here rather than through application code?
  • How would you distinguish healthy Caddy from the failure “publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol” using curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto?
  • When would this trade-off-“Nginx gives explicit high-performance control; Caddy trades fewer knobs for a simpler certificate lifecycle”-justify a different Reverse Proxy design?
Interview tip: Give one concrete operation-Caddyfile: app.example.com { reverse_proxy api:8080 }; docker run --network edge -p 443:443 caddy:2-and one proof-curl --resolve app.example.com:443:127.0.0.1 https://app.example.com/health -k and inspect api access logs for original host/proto. Then name the boundary: publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Reverse Proxying Containers with Nginx & Caddy with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Custom Subnets, Gateways & Static Container IP Assignment
Next topic
Automatic SSL/TLS & Dynamic Routing with Traefik