BlogLinux and server operations

NGINX 502 Bad Gateway: Trace the Upstream Failure

When NGINX generates a 502 Bad Gateway response, it could not obtain a valid upstream response for the request. An upstream can also return its own 502, so identify which component generated the error. The useful next step is to match one failing request to the error log and the configured upstream. Restarting every service or increasing all timeouts can obscure the cause without correcting it.

This workflow is for an administrator of an NGINX reverse proxy or FastCGI setup. A CDN-branded error may come from another gateway, so establish where the response originated first. Commands use a Linux system with systemd; unit names, ports and paths must be taken from your installed configuration.

1. Capture one reproducible request

Record the hostname, URL path, time and whether the failure affects all requests or one operation. Use a safe read-only page rather than replaying a payment, form submission or administrative action. Compare the public path with an authorized internal probe only when the architecture permits it.

curl --silent --show-error --connect-timeout 5 --max-time 15 \
  --output /dev/null --write-out 'HTTP: %{http_code}\n' \
  'https://www.example.com/'

Preserve the error and matching timestamp before restarting. If the application is intermittently failing, note whether errors coincide with deployments, worker restarts or heavy requests. One successful homepage request does not clear an intermittent fault.

2. Read the configuration NGINX would load

sudo nginx -t
sudo nginx -T

The first command checks syntax and access to referenced configuration files; the second also prints the assembled configuration. It can reveal internal hostnames or embedded credentials, so inspect it privately and redact it before sharing. Neither command proves the application is healthy. The official NGINX command reference defines their behavior.

Find the server and location that match the failing request, its error_log destination, and the upstream directive. Configuration on disk may differ from what an already-running worker loaded after an unsuccessful reload. Use the deployment and service logs to establish the active state.

  • proxy_pass connects to an HTTP or HTTPS upstream; check its exact address, scheme, port and expected Host header.

  • fastcgi_pass connects to a FastCGI service such as PHP-FPM; it is not an HTTP endpoint.

  • A Unix-domain socket has a filesystem path and access permissions instead of a TCP port.

The proxy module and FastCGI module have different directives. Do not copy a proxy timeout setting into a FastCGI diagnosis and assume it affects the request.

3. Use the log message to choose the next check

Log clue

Likely investigation

Avoid assuming

Connection refused

No listener at the selected address, a stopped process or an incorrect endpoint

That more CPU or a longer timeout will create the missing listener.

No such file or directory for a socket

Socket path changed, service failed to create it, or namespaces differ

That creating an empty file will create a working socket.

Permission denied

Worker identity, parent-directory traversal, socket ownership and security policy

That chmod 777 or disabling a security policy is an acceptable fix.

Prematurely closed connection

Application crash, termination, protocol mismatch or dependency failure

That the proxy is necessarily the source of the fault.

Invalid upstream response or oversized header

Protocol and application response, then the relevant documented limits

That every 502 has the same cause.

These are investigation branches, not diagnoses from a single phrase. Match the line’s upstream and request to the reported incident. A timeout may appear as a different gateway status, including 504, depending on the failing layer; increasing a timeout does not repair a refused connection.

4. Check the service and listener

Replace example-app.service with the unit that owns the identified upstream:

sudo systemctl status example-app.service --no-pager
sudo journalctl -u example-app.service --since '-15 minutes' --no-pager
sudo ss -ltnp
sudo ss -lxnp

The TCP and Unix-socket listings show which endpoints are listening. Inspect the relevant entry rather than publishing the entire output. A process marked active can still serve errors or listen on the wrong address. The systemctl reference distinguishes service state from application behavior.

For an ordinary loopback HTTP upstream, an example read-only probe is:

curl --silent --show-error --connect-timeout 3 --max-time 10 \
  -H 'Host: www.example.com' --output /dev/null \
  --write-out 'Upstream HTTP: %{http_code}\n' \
  'http://127.0.0.1:8080/'

Use the actual endpoint and the headers the application legitimately expects. Do not add trusted-proxy headers to bypass an access boundary. Do not send an HTTP curl request to a PHP-FPM FastCGI port and interpret the result as an application health check.

5. Resolve a Unix-socket or PHP-FPM mismatch narrowly

Compare NGINX’s configured socket with the pool’s listen value and the socket actually present on disk. Inspect the NGINX worker account and permissions on each parent directory. For PHP-FPM, listen.owner, listen.group, listen.mode and configured ACLs determine socket access; the PHP-FPM configuration reference describes their interaction.

Use the service or pool configuration to make a persistent correction. Hand-editing a temporary socket’s permissions may be undone when the service recreates it. Preserve the intended separation between sites and service accounts. If the process is being killed or crashing, investigate its logs and resource evidence before changing access rules.

6. Validate one correction and keep rollback available

Save the known previous configuration and change only the identified fault. Run the relevant configuration test, then use the application’s supported reload or restart procedure. If NGINX configuration changed, nginx -t must pass before reloading it. Do not restart a healthy database merely because the gateway returned 502.

Repeat the original failing request, an ordinary page and a representative dynamic function. Confirm that the matching error no longer appears over the workload period that previously triggered it. Record the changed endpoint, service or permission, plus the evidence that supports the diagnosis.

If your team needs help operating the web stack, use our managed server hosting scope to agree which NGINX and application services are covered. Provide the incident record and define who approves changes and verifies recovery. Custom application fixes require explicit agreement; a management label alone does not transfer every debugging task.

If you still need wider resource analysis, continue with the VPS performance diagnostic guide. For the underlying request roles, see web servers versus application servers. This keeps a specific upstream failure separate from a general hosting-capacity decision.

References and further reading

Documentation reviewed on 29 November 2026. This is a documentation-based operating guide, not a report of a customer incident or a benchmark. Examples are illustrative; verify paths, permissions and installed command options in your own environment.

Published

Linux and server operations · Performance and networking