Nginx Proxy Manager: 502 Bad Gateway — Fix
The Problem
When accessing a site proxied through Nginx Proxy Manager (NPM), the browser shows 502 Bad Gateway. The NPM admin UI at port 81 works fine, but proxied services return 502 errors.
Updated March 2026: Verified with latest Docker images and configurations.
The Cause
A 502 means NPM received an invalid or no response from the upstream (backend) server. Common causes:
- Wrong forward hostname — using
localhostinstead of the Docker container name or host IP - Wrong forward port — using the host-mapped port instead of the container’s internal port
- Container not on the same Docker network as NPM
- Backend service not yet started or has crashed
- SSL scheme mismatch — forwarding to HTTPS when the backend only speaks HTTP (or vice versa)
The Fix
Method 1: Use Container Name as Hostname
If NPM and the backend service are in Docker, use the container name (not localhost or the host IP):
- Forward Hostname:
my-app(the container name) - Forward Port:
8080(the internal container port, not the host-mapped port)
Example: If your app is ports: "3000:8080", use port 8080 in NPM, not 3000.
Method 2: Fix Docker Network
Both NPM and the backend must share a Docker network:
# In NPM's docker-compose.yml
services:
npm:
image: jc21/nginx-proxy-manager:2.14.0
networks:
- proxy
# In your app's docker-compose.yml
services:
myapp:
image: myapp:latest
networks:
- proxy
networks:
proxy:
external: true
Create the shared network: docker network create proxy
Method 3: Use Host IP for Non-Docker Services
If the backend runs directly on the host (not in Docker), use the host’s IP address:
- On Linux: use
172.17.0.1(Docker bridge gateway) or your server’s LAN IP - Or add
extra_hosts: ["host.docker.internal:host-gateway"]to NPM’s compose file and usehost.docker.internalas the hostname
Method 4: Fix the Scheme
In the NPM Proxy Host edit screen, check “Scheme”:
- Use http if the backend serves plain HTTP (most containers)
- Use https only if the backend itself serves HTTPS (uncommon)
Prevention
- Always use container names as forward hostnames for Docker services
- Use internal ports (not host-mapped ports) in NPM proxy host config
- Put all proxied services on a shared Docker network with NPM
- Verify the backend is running:
docker logs <container-name>
Related
Get self-hosting tips in your inbox
Get the Docker Compose configs, hardware picks, and setup shortcuts we don't put in articles. Weekly. No spam.
Comments