Fix Nginx 502 Bad Gateway — Upstream Connection Failed
Direct answer
Nginx returns 502 Bad Gateway when it cannot get a valid response from the upstream server. This means the backend process is either down, unreachable, or timing out. Check the upstream process status and Nginx error logs.
Structured breakdown
Cause
A 502 Bad Gateway from Nginx means the upstream server (your app) is not responding. Check if the upstream process is running, verify the proxy_pass address, and inspect Nginx error logs.
Fix
- Check if the upstream application is running: systemctl status <service> or ps aux
- Verify proxy_pass in Nginx config points to the correct host and port
- Check Nginx error logs: tail -f /var/log/nginx/error.log
Outcome
Upstream responses are received correctly and 502 errors stop.
Common causes
- Upstream application process crashed or is not running
- Incorrect proxy_pass address or port in Nginx config
- Upstream server is overloaded and dropping connections
- PHP-FPM, Node.js, or application process pool exhausted
- Firewall or security group blocking Nginx-to-upstream traffic
Fix steps
- 1
Check if the upstream application is running: systemctl status <service> or ps aux
- 2
Verify proxy_pass in Nginx config points to the correct host and port
- 3
Check Nginx error logs: tail -f /var/log/nginx/error.log
- 4
Increase upstream timeout values: proxy_connect_timeout, proxy_read_timeout
- 5
Restart the upstream service and monitor for stability
Analyze this issue
Paste the issue description, logs, or symptoms into the fix tool to inspect this problem with your own runtime details.
Need more context?
If the standard steps do not resolve the issue, open the fix tool and include the current logs, configuration, and deployment changes.
Open Fix ToolFrequently asked questions
Related technical context
These examples show the commands, logs, and configuration patterns most often used to verify this issue.
Command examples
tail -f /var/log/nginx/error.logsystemctl status <upstream-service>curl -I http://127.0.0.1:3000/health
Log snippet
upstream prematurely closed connection while reading response header from upstream
connect() failed (111: Connection refused) while connecting to upstreamConfig snippet
upstream backend {
server 127.0.0.1:3000;
}
server {
location / {
proxy_pass http://backend;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
}
}