Nginx
Nov 20, 2025 5 min read

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

bash
curl -v http://127.0.0.1:8080/health

Allow outbound connections (SELinux)

bash
sudo setsebool -P httpd_can_network_connect 1

Tune upstream timeouts

nginx
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

Weekly digest

One DevOps fix in your inbox each week

Short, practical, no fluff. Real errors, real fixes — straight from production postmortems.