NGINX 502 Bad Gateway: 5 Checks That Find It Fast

Symptom

Browser shows "502 Bad Gateway"; upstream logs show nothing received.

Root cause

NGINX accepted the request but the backend (php-fpm, Node, Python, another proxy) did not return a valid response.

Fix, in order

1. Confirm the backend is listening where NGINX expects itcurl -v http://127.0.0.1:8000/
2. Check the upstream block matches the backend port/socketgrep -R "upstream" /etc/nginx/ && grep -R "proxy_pass\|fastcgi_pass" /etc/nginx/
3. Read the actual error from NGINXtail -n 50 /var/log/nginx/error.log
4. Verify PHP-FPM socket exists and has permissionls -la /run/php/ # should match fastcgi_pass path
5. If the backend is in Docker, confirm container port publishingdocker ps --format "table {{.Names}}\t{{.Ports}}"

Prevent it coming back

Health-check upstreams and fail fast; alert on error-log lines containing "upstream".

Deep-dive article

Full walkthrough: NGINX 502 Bad Gateway: 5 Checks That Find It Fast on the NinjaOps blog.

Run it on clean infra

Spinning up a fresh box to reproduce or escape this error?

Spin up a clean test server

Related

Cloud & DevOps tool comparisons · Hardware build guides

NinjaOps publishes free engineering guides. Some outbound links are affiliate links: they cost you nothing and support the site.

Last reviewed 2026-10-02 · NinjaOps SEO grid · DevOps blog