Fix Port Conflicts & 502 Bad Gateway Errors
How to trace Address already in use and Nginx 502 Bad Gateway errors using ss, loopback binding checks, and upstream port alignment.
You'll learn how to find the exact process holding a port with ss -tulpn, why IPv4 vs IPv6 loopback mismatches (127.0.0.1 vs ::1) trigger 502 Bad Gateway, and how to align Nginx proxy_pass with your backend.
1. The big idea
Address already in use and 502 Bad Gateway are two sides of the same coin: socket mismatches. Either another process already owns the port your app wants to bind, or your reverse proxy (Nginx/Caddy) is forwarding requests to a port, IP family, or Unix socket where nothing is listening.
2. Find what is actually listening with ss
Skip deprecated netstat and use ss from iproute2 to see every listening TCP/UDP socket and its owning PID:
# Show listening TCP/UDP ports with process names and PIDs
sudo ss -tulpn
# Filter for a specific port (e.g., 3000 or 8080)
sudo ss -tulpn | grep :3000Look closely at the Local Address:Port column:
127.0.0.1:3000— listening on IPv4 loopback only[::1]:3000— listening on IPv6 loopback only0.0.0.0:3000or*:3000— listening on all interfaces
3. Fixing Nginx 502 Bad Gateway mismatches
A classic 502 Bad Gateway happens when Nginx proxies to http://localhost:3000:
localhostresolves to[::1](IPv6) first on modern Linux, while your Go or Node app bound only to127.0.0.1:3000(IPv4)—or vice versa.- Result: Nginx gets
Connection refusedon[::1]:3000and returns502 Bad Gatewayto the browser.
Fix it by matching explicit IPv4 loopback addresses in both your app and Nginx:
server {
listen 80;
server_name api.example.com;
location / {
# Use explicit 127.0.0.1 instead of ambiguous "localhost"
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Test and reload Nginx, and watch the error log if 502 persists:
sudo nginx -t && sudo systemctl reload nginx
sudo tail -f /var/log/nginx/error.logVerify with ss -tulpn that your upstream service is listening on the exact IP family and port your proxy_pass targets, and confirm nginx -t passes cleanly.
4. Common pitfalls
| Mistake | Why it fails | Better fix |
|---|---|---|
Using proxy_pass http://localhost:3000 | localhost may resolve to ::1 while app binds 127.0.0.1 | Use explicit http://127.0.0.1:3000 |
Binding backend to 0.0.0.0:3000 unnecessarily | Exposes raw app port publicly bypassing Nginx TLS/auth | Bind internal upstreams to 127.0.0.1 |
| Killing PID without checking supervisor | systemd immediately respawns the conflicting process | Stop via sudo systemctl stop <service> |
Ignoring error.log | 502 hides whether error is refused, timeout, or SELinux | Tail /var/log/nginx/error.log while reproducing |
5. What you learned
You now know how to inspect live sockets with ss -tulpn, eliminate IPv4/IPv6 localhost ambiguity in Nginx proxy_pass, and read /var/log/nginx/error.log to resolve 502 Bad Gateway errors fast.