Docker Nginx 502 Bad Gateway Fix
In this tutorial, you'll learn about Docker Nginx 502 Bad Gateway Fix. We cover key concepts, practical examples, and best practices.
A 502 Bad Gateway from Nginx in Docker means Nginx cannot reach the upstream application container. This happens when the application container is down, the port is wrong, the network is misconfigured, or the application binds to localhost instead of 0.0.0.0.
The Problem
curl http://localhost
Returns:
<html>
<head><title>502 Bad Gateway</title></head>
Nginx logs show:
2026/06/24 10:30:00 [error] connect() failed (111: Connection refused) while connecting to upstream
Wrong Approach
# WRONG β Nginx on host network, app on bridge with wrong upstream
services:
nginx:
image: nginx
ports:
- "80:80"
app:
image: my-app:latest
ports:
- "3000:3000" # Unnecessary β Nginx communicates over internal network
Right Approach
# RIGHT β same network, correct upstream
services:
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- app
app:
image: my-app:latest
expose:
- "3000"
environment:
- HOST=0.0.0.0 # Bind to all interfaces
With Nginx config:
upstream app {
server app:3000;
}
server {
listen 80;
location / {
proxy_pass http://app;
}
}
Step-by-Step Fix
Step 1: Check the upstream container is running
docker ps --filter name=app
Step 2: Check the application logs
docker logs app --tail 50
Step 3: Test connectivity from Nginx container
docker exec nginx curl -v http://app:3000
If this fails, the network or hostname is wrong.
Step 4: Ensure the application binds to 0.0.0.0
# WRONG β binds to localhost only
app.run(host='127.0.0.1', port=3000)
# RIGHT β binds to all interfaces
app.run(host='0.0.0.0', port=3000)
Step 5: Check network configuration
docker network ls
docker inspect app --format '{{.NetworkSettings.Networks}}'
Step 6: Verify Nginx config inside container
docker exec nginx nginx -t
docker exec nginx cat /etc/nginx/conf.d/default.conf
Prevention Tips
- Always bind applications to
0.0.0.0inside containers - Use service names (not IPs) for upstream definitions
- Place all services on the same Docker network
- Add healthchecks to upstream services
- Test with
curlfrom the Nginx container before debugging the browser
Common Mistakes with nginx 502
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world DOCKER code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro