# How to Fix HTTP 502 Bad Gateway (Caddy / Nginx → n8n) (2026 Verified Ubuntu/Docker Fix)

- URL: https://nodebudget.dev/errors/n8n-webhook-502-bad-gateway-caddy/
- Category: HTTPS & Caddy · Severity: High
- Author: Yuanqiang Leng (https://github.com/a895047059)
- Updated: 2026-11-26

## Exact Log Signature
`502 Bad Gateway | dial tcp: lookup n8n: no such host | connection refused`

## Root Cause
Reverse proxy is pointing to 127.0.0.1:5678 inside another container, or n8n crashed in a restart loop.

## Verified Fix Commands
```bash
docker compose ps && docker compose logs --tail=50 n8n
# In Caddyfile, proxy to Docker service name (NOT localhost):
# reverse_proxy n8n:5678
```

## Permanent Production Prevention
Keep Caddy and n8n on the same Docker Compose bridge network.

---
Source: NodeBudget (https://nodebudget.dev/tools/error-decoder/)
