Nginx 502 Bad Gateway: What It Means and How to Fix It
A practical guide to diagnosing and fixing Nginx 502 Bad Gateway errors, with real examples and quick fixes.
March 22, 2026 · 5 min · 1008 words · Rob Washington
Table of Contents
You refresh your page and see it: 502 Bad Gateway. Nginx is telling you something went wrong, but what? This guide covers the most common causes and how to fix each one.
# Is your upstream running?systemctl status your-app
# ordocker ps | grep your-app
# Can you reach it directly?curl -v http://localhost:3000/health
# What do the logs say?tail -50 /var/log/nginx/error.log
journalctl -u your-app --since "5 minutes ago"
On RHEL/CentOS, SELinux may block Nginx from connecting to upstream ports.
Symptoms:
Everything looks correct
Works when you disable SELinux
Logs show permission denied
Fix:
1
2
3
4
5
6
7
8
# Check if SELinux is the problemausearch -m avc -ts recent | grep nginx
# Allow Nginx to connect to any portsetsebool -P httpd_can_network_connect 1# Or allow specific portsemanage port -a -t http_port_t -p tcp 3000
#!/bin/bash
echo"=== Nginx Status ==="systemctl status nginx
echo"=== Upstream Status ==="systemctl status your-app
echo"=== Port Bindings ==="ss -tlnp | grep -E ':(80|443|3000)'echo"=== Recent Nginx Errors ==="tail -20 /var/log/nginx/error.log
echo"=== Direct Upstream Test ==="curl -v http://127.0.0.1:3000/health