Skip to content

VPS

Diagnosing NGINX 502 Bad Gateway on a VPS

Check the configured upstream, application listener and matching error log before increasing timeouts.

NGINX502Reverse proxy
Reading time
2 min read
Published
Updated
Maintained by
EnderHost Team

A 502 from an NGINX reverse proxy usually calls for checking the upstream application and the matching error log. The web server may be reachable while the application behind it is stopped, listening elsewhere or returning an invalid response.

Find the failing hop

  1. Record the failing hostname, path and exact time.
  2. Determine whether the response comes from your NGINX server or another proxy in front of it.
  3. Read the corresponding NGINX error entry.
  4. Compare the configured upstream address, port and protocol with the application listener.
  5. Check the application’s service status and logs for the same interval.
Error context
ClueInvestigation
Connection refusedApplication is stopped or listening on another address or port
Permission denied on a socketSocket ownership, mode and security policy
Upstream connection closes earlyApplication crash or protocol mismatch
Only one path failsIts location rule and backend handler

Test from the proxy’s environment

Where appropriate, make a small request directly to the configured upstream from the proxy host. Use the protocol the backend actually speaks. With containers, localhost refers to the current container or host namespace; it may not refer to the application container you intended.

Keep the backend restricted as designed. Opening its port publicly may hide a routing mistake while creating unwanted exposure. Compare the actual upstream configuration rather than copying a generic proxy block over the site.

Validate a targeted fix

sudo nginx -t

This checks NGINX configuration syntax and referenced files. If it succeeds after a reviewed change, reload using your distribution’s service manager. A syntax pass does not prove the backend responds correctly, so repeat the failing request and inspect fresh logs.

Use service startup diagnosis when the application is down. Check disk capacity if it cannot write logs or temporary files.

Sources and references

Hosting documentation. Publication and update dates reflect this edition.

Related articles

Related topics

Hardware we trust. Software you know.

PREPARING YOUR EXPERIENCE