Reverse Proxying Containers with Nginx & Caddy
Front containers with Nginx or Caddy for TLS termination and path-based routing.
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:2for 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
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.
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, 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.
Exercise this failure safely: publishing the backend port too and bypassing TLS/proxy policy, or terminating TLS but forgetting forwarded client protocol.
How it works, step by step
Step 1 of 41. 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.
- 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/protoin 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
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.
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.
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:2as proof and skippingcurl --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:2address 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?
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
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.