Cloud & Operations · Tutorials

Troubleshooting 502 Bad Gateway: From Error Log to Upstream Process

This tutorial works through a 502 in order: read the Nginx error log to decide between connection failure and timeout, confirm whether php-fpm or the backend process is alive, verify the socket or port Nginx points at, then find out why the upstream exited. Every step includes the command and the decision it supports, so you do not change configuration on a hunch.

Estimated time: About 15 minutesService: Technical Operations Support

Steps

  1. Classify with the error log

    Run tail -n 200 /var/log/nginx/error.log and read the last messages. connect() failed (111: Connection refused) means the upstream cannot be reached; upstream timed out means it connected but did not answer in time; no live upstreams means every backend was removed by health checks. Each case leads somewhere different.

  2. Confirm the upstream is running

    For PHP, run ps aux | grep php-fpm to see workers, then check the listening address with ss -lntp or ls -l. TCP appears as 127.0.0.1:9000 and a Unix socket as a .sock file. If the process is gone, 502 is unavoidable: fix the process first.

  3. Verify the address Nginx uses

    Run nginx -T | grep -n fastcgi_pass (or proxy_pass) and compare the target character by character with the listening address confirmed above. After a PHP version change or a migration, a stale socket path is the most common mismatch.

  4. Find out why the upstream exited

    Check the php-fpm error and slow logs, then run dmesg -T | grep -i "killed process" to see whether the OOM killer terminated workers. Memory exhaustion is the most overlooked cause. For Java or Node, read the tail of the application startup log.

  5. Decide between saturation and crash

    Intermittent 502s usually mean the worker pool is saturated or memory is leaking: check whether pm.max_children stays maxed out and whether process memory keeps climbing. Persistent 502s usually mean a configuration mismatch or a process that will not start.

  6. Recover and observe

    Change only the one setting that explains the failure, validate with nginx -t, reload, then verify with repeated curl -I requests and watch the error log for ten minutes. Changing several parameters at once makes it impossible to know what fixed it.

Practical tips

  • Classify before you act: the log already says refused or timed out
  • Restarting php-fpm is relief, not a fix, unless you address OOM or saturation
  • With multiple backends, verify each one instead of assuming one fix covers all

FAQ

Should 502 and 504 be handled differently?

Yes. 502 is a failed or invalid upstream response, so check processes and ports. 504 is a timeout, so check slow endpoints and timeout settings.

Does a config change need a restart or a reload?

Nginx needs a reload. Changes to php.ini or an fpm pool require restarting the php-fpm pool.

Related news

Explain your case first, then confirm the scope

Tell us your entity, business model and current progress. We will confirm what we can assist with and what you need to do before discussing fees.