Skip to main content

Fix Caddy 502 Bad Gateway and HTTPS errors with Docker

Updated 7 min readMarkdown

When Caddy runs inside Docker on a VPS and returns 502 Bad Gateway or fails to issue an HTTPS certificate, the root cause is rarely Caddy itself: in Caddy's reverse_proxy (v2.11), any failure dialing the upstream becomes HTTP 502 (while timeouts become 504), and any firewall block on ports 80/443 or DNS mismatch stops ACME validation cold.

Use this guide to map the exact Caddy log line to its root cause in under five minutes and fix the six issues behind almost every Docker Compose proxy outage.

Quick lookup: log message → root cause

Start by reading Caddy's recent logs from your Compose directory:

docker compose logs --tail=80 caddy

Match what you see against this table:

Caddy log or browser error Most likely cause Immediate fix
dial tcp 127.0.0.1:PORT: connect: connection refused Caddyfile points to localhost or 127.0.0.1 inside the Caddy container Switch to service-name:PORT on the same Docker network
lookup app on 127.0.0.11:53: no such host Caddy and the app are on different Docker networks (or the app is stopped) Attach both to a shared network (external: true) and check docker compose ps
dial tcp 172.x.x.x:PORT: connect: connection refused The app inside its container binds only to 127.0.0.1 (or is restarting) Bind the app to 0.0.0.0 and verify it was not killed with OOMKilled
context deadline exceeded (HTTP 504) Frozen upstream, slow database or wrong internal port Test the upstream from inside the Caddy container and inspect app logs/CPU
tls: internal error / ERR_SSL_PROTOCOL_ERROR ACME challenge failed (port 80/443 blocked, wrong DNS or stale AAAA record) Open 80/tcp and 443, check dig A/AAAA and free port 80 on the host
ERR_TOO_MANY_REDIRECTS or Cloudflare 525 / 526 Cloudflare SSL mode is Flexible (or origin cert not yet issued) Set Cloudflare SSL/TLS to Full (strict); temporarily use DNS-only if needed

Diagnostic flow for Caddy with Docker on a VPS showing the six checkpoints that cause 502 Bad Gateway and HTTPS certificate failures

1. reverse_proxy localhost:PORT inside the container

This is the single most common mistake when moving a Caddyfile from bare-metal Ubuntu into Docker. Inside the caddy container, localhost and 127.0.0.1 refer to the Caddy container itself, not the VPS host or your app container.

# Wrong inside Docker Compose
app.example.com {
	reverse_proxy localhost:5678
}

# Correct: use the Compose service name
app.example.com {
	reverse_proxy n8n:5678
}

If the upstream genuinely runs outside Docker (directly on the VPS host listening on 0.0.0.0:3000 or the Docker bridge), add extra_hosts: ["host.docker.internal:host-gateway"] to the caddy service, allow 3000/tcp from the Docker bridge interface in ufw if the firewall is active, and point reverse_proxy to host.docker.internal:3000.

2. The app binds only to 127.0.0.1 inside its container

Even with the right service name (reverse_proxy app:3000), Caddy logs dial tcp 172.18.0.4:3000: connect: connection refused when the process inside the upstream container listens only on loopback (127.0.0.1). Tools like Vite, Uvicorn, Flask and several Node/Go servers do this unless HOST is explicitly set.

Configure the upstream container to bind to 0.0.0.0 while keeping its port unpublished on the host:

services:
  app:
    image: your-app:latest
    environment:
      HOST: "0.0.0.0"
      PORT: "3000"
    # No "ports: 3000:3000": reachable only through the internal Docker network
    networks:
      - web

Test connectivity directly from inside the Caddy container:

docker compose exec caddy wget -S --spider http://app:3000/ 2>&1 | head -n 15

3. Caddy and the app live in separate Compose stacks

When Caddy lives in /opt/caddy/docker-compose.yml and your app lives in /opt/n8n/docker-compose.yml, each Compose project creates its own isolated bridge network. Docker's embedded DNS (127.0.0.11) returns no such host because neither stack can resolve the other.

Create one shared bridge network on the host:

docker network create caddy_net

Then attach both docker-compose.yml files to it as an external network:

services:
  caddy:
    image: caddy:2.11-alpine
    networks:
      - caddy_net

networks:
  caddy_net:
    external: true

Add caddy_net to the app's docker-compose.yml as well — both under the service (networks: [default, caddy_net]) and in the top-level networks: block (caddy_net: { external: true }) — and run docker compose up -d in both directories.

4. Upstream container crash-looping or OOMKilled

An intermittent 502 Bad Gateway — working for a few minutes and then dropping — usually means the upstream container is crashing on boot or getting killed by the Linux kernel for running out of RAM (exit code 137).

Check container state, exit codes, restart counts and OOM flags (including stopped containers) in one command:

docker compose ps -a
docker inspect --format '{{.Name}} | status={{.State.Status}} | exit={{.State.ExitCode}} | oom={{.State.OOMKilled}} | restarts={{.RestartCount}}' $(docker compose ps -aq)

If oom=true shows up, raise the container's mem_limit or upgrade the VPS tier. See How much RAM and vCPU for n8n, Evolution API and Coolify or Sizing a 24 GB or 48 GB VPS for PostgreSQL, Redis and n8n.

5. HTTPS certificate fails: port 80/443 conflict, firewall or stale AAAA record

To issue a Let's Encrypt or ZeroSSL certificate, Caddy must answer the HTTP-01 challenge on port 80 or TLS-ALPN-01 on port 443 from the public internet. Four issues block this most often:

  1. Another web server already holding ports 80 or 443: check what owns those sockets on the host:
    sudo ss -tulpn | grep -E ':(80|443)\s'
    
    If nginx or apache2 is running on the host, disable it (sudo systemctl disable --now nginx apache2).
  2. Firewall (ufw or cloud panel firewall) blocking port 80: even if you only serve HTTPS, HTTP-01 requires 80/tcp open alongside 443/tcp and 443/udp (for HTTP/3):
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw allow 443/udp
    
  3. Stale DNS AAAA (IPv6) record pointing to an old server: Let's Encrypt prefers IPv6 whenever an AAAA record exists. Updating only the A record to your new VPS IPv4 leaves validation hitting the old IPv6 address. Check both:
    dig +short A app.example.com
    dig +short AAAA app.example.com
    
    If AAAA returns an address that does not belong to this VPS, delete that record at your DNS provider.
  4. Missing persistent /data volume: without caddy_data:/data mounted in docker-compose.yml, every container recreate requests a fresh certificate until you hit Let's Encrypt rate limits.

6. Cloudflare proxy in front: ERR_TOO_MANY_REDIRECTS, 525 or 526

When your domain sits behind Cloudflare's orange-cloud proxy, two settings keep Caddy working cleanly:

  • Set Cloudflare SSL/TLS mode to Full (strict): in Flexible mode, Cloudflare talks to your VPS over plain HTTP on port 80, Caddy redirects to https://, and Cloudflare bounces that redirect back to the browser in an infinite loop (ERR_TOO_MANY_REDIRECTS).
  • First-time issuance blocked by proxy or WAF: if Cloudflare returns 525 (SSL handshake failed) or 526 (Invalid SSL certificate) before Caddy has obtained its initial certificate, switch the DNS record temporarily to DNS only (grey cloud), confirm Caddy issues the cert (curl -Iv https://app.example.com), and then re-enable the orange cloud with Full (strict).

Validate your Caddyfile before reloading. A stray brace or indentation typo can abort reloads quietly. Run docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile and reload with zero downtime via docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile.

1-minute diagnostic checklist

Run these five checks on the VPS (replacing app.example.com with your domain) to pinpoint networking, DNS, port and TLS issues at once:

# 1. Validate Caddyfile syntax
docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile

# 2. Check ports 80 and 443 on the host
sudo ss -tulpn | grep -E ':(80|443)\s'

# 3. Verify public IPv4 and IPv6 DNS records
dig +short A app.example.com && dig +short AAAA app.example.com

# 4. Inspect container status and restarts
docker compose ps -a

# 5. Test TLS handshake and HTTP status
curl -Iv --max-time 10 https://app.example.com/ 2>&1 | grep -E 'Connected to|SSL certificate|subject:|< HTTP/'

Next steps


Originally published at https://nuvits.net/en/blog/fix-caddy-docker-502-bad-gateway-https-vps

No comments yet. Be the first to comment.