TR EN

Fix Nginx 502 Bad Gateway Error with PHP-FPM / Upstream

A 502 response points to a gateway/upstream problem; identify the failing path and service before changing configuration or restarting anything.

Start with the response and request path

A 502 means a gateway or proxy encountered a problem with an upstream response; the status alone does not identify the failed service. Note the affected URL path, time, request ID if available, whether failures are intermittent, and which proxy or CDN returned the response. Do not share query strings, cookies, tokens or request bodies.

1. Correlate the Nginx error

Inspect your configured Nginx error log around that time. Its location varies by deployment; the following is a local example, not a universal path:

sudo tail -n 50 /var/log/nginx/error.log

connect() failed with a refused connection points toward the reported upstream address or listener; no such file for a Unix socket suggests checking its path and existence; permission denied calls for examining socket access and mandatory access controls; upstream prematurely closed connection calls for correlating application logs. These are leads, not proven root causes. Logs can contain sensitive data; redact before sharing.

2. Identify the actual upstream

Follow the relevant virtual host and location to its proxy_pass or fastcgi_pass. On the Nginx host, sudo nginx -t tests syntax and referenced files; sudo nginx -T additionally prints the configuration. The latter can expose internal names and secrets: inspect locally, do not post its output. See the Nginx command documentation. Compare the actual configured socket or address with the service listener; account for container network namespaces and service names.

3. Check the matching service

Use your deployment's actual service name and version to inspect its status and logs. For PHP-FPM, compare the pool listen setting with Nginx fastcgi_pass, and review Unix socket owner, group and mode. PHP-FPM's pool documentation describes listener and worker limits. An exposed FastCGI endpoint is dangerous; do not open it to the internet. For Node or Python, inspect that specific application's listener and logs instead of assuming PHP-FPM.

4. Make only the justified change

For an overloaded pool, correlate queue, worker-limit and memory evidence before changing worker counts; adding workers without capacity can worsen failures. Do not blindly restart a service, copy a fixed PHP socket path, disable access controls or increase timeouts. If a configuration change is warranted, prepare a rollback, validate it using the relevant configuration test and follow your deployment procedure.

Retry a safe, authorized request on the same path and compare both proxy and upstream results. Avoid replaying state-changing requests or real user payloads. A website-level check from our 502 reference does not inspect private upstreams or prove the cause.

Behind a CDN?

A CDN-branded 502 does not by itself establish which component caused it. Use the provider's response details and correlate with origin logs; see Cloudflare's 502/504 guidance. Reviewed 15 November 2026.

Frequently Asked Questions

Is 502 Bad Gateway a client or server-side issue?
502 is a gateway response indicating an upstream communication problem. It does not prove the cause, which service failed, or whether a request is otherwise valid.
What does a Cloudflare 502 error indicate?
Cloudflare or an origin can generate a 502. Use the provider’s response details and correlate origin logs rather than assuming a specific failure.