Fix Nginx 502 Bad Gateway
Nginx is healthy, but every request 502s. The upstream is the suspect — here's how to confirm and fix.
Problem
Browsers return `502 Bad Gateway` from Nginx. `nginx -t` is clean, the process is running, but no requests succeed.
Root cause
- Upstream service is down or listening on a different port.
- SELinux denies Nginx from making outbound connections.
- Upstream closes the connection (worker timeout, OOM).
- Wrong `proxy_pass` scheme (https vs http).
Solution
Confirm the upstream from Nginx's perspective
curl -v http://127.0.0.1:8080/healthAllow outbound connections (SELinux)
sudo setsebool -P httpd_can_network_connect 1Tune upstream timeouts
location / {
proxy_pass http://app;
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
proxy_next_upstream error timeout http_502 http_503;
}Frequently asked questions
Related fixes
Nginx
5 minFix Nginx SSL Handshake Failures
TLS errors from clients or curl, and how to read OpenSSL output to pinpoint the cause.
#ssl#tls#openssl
Nginx
3 minFix Nginx 413 Request Entity Too Large
Allow larger uploads by raising `client_max_body_size` in the right server block.
#uploads#client_max_body_size
Weekly digest
One DevOps fix in your inbox each week
Short, practical, no fluff. Real errors, real fixes — straight from production postmortems.