Nginx 502 Bad Gateway Fix

A 502 usually means Nginx reached a broken or unavailable upstream. Check the upstream service before changing redirect rules.

Symptoms

  • Browser shows 502 Bad Gateway.
  • Nginx error log mentions connect failed or upstream prematurely closed connection.
  • The app works locally but fails behind Nginx.

Likely causes

  • Upstream service is down or listening on a different port.
  • PHP-FPM socket path is wrong.
  • Container name or internal DNS is wrong.
  • Upstream crashes before returning a response.

Fix steps

  1. Check nginx -t and reload only after syntax passes.
  2. Inspect Nginx error logs and upstream app logs.
  3. Verify upstream host, port or socket path.
  4. Test the upstream directly with curl or a local socket check.

Verify the fix

  • Run curl -I against the public URL.
  • Run curl from the server to the upstream.
  • Confirm logs stop producing upstream errors.

FAQ

Is 502 an SSL problem?

Sometimes proxy TLS can be involved, but most 502 cases start with the upstream service.

Can nginx -t detect bad upstream runtime state?

No. It checks syntax, not whether the app is healthy.

Related tools and guides

Last updated: May 18, 2026