Back to Blog
DevOpsJune 30, 20264 min read

Nginx 502 Bad Gateway: How to Troubleshoot Upstream Issues

Fixing an nginx 502 bad gateway error starts with your logs. Learn how to troubleshoot upstream connections and resolve common configuration pitfalls.

nginxdevopslinuxtroubleshootingweb-serversnetworking

Waking up to an alert that your production site is returning an nginx 502 bad gateway is never fun. It usually means your web server is alive, but the application service behind it—the upstream—is either dead, misconfigured, or refusing to talk.

In my experience, 90% of these cases boil down to a simple connectivity mismatch between Nginx and the backend, like a Node.js process crashing or a Docker container losing its network route. Here is how I methodically track down the root cause.

The First Stop: Nginx Error Log Analysis

Never guess when you can read the logs. The default Nginx error log is your best friend when you need to perform nginx error log analysis.

Go straight to /var/log/nginx/error.log and look for lines containing "502" and "upstream". You will typically see one of two patterns:

  1. "Connection refused": Nginx tried to connect, but nothing was listening on the specified port.
  2. "Connection reset by peer" or "Premature end of response": The backend started a handshake but dropped it mid-way.

If you are dealing with a flood of logs, use a quick tail -f /var/log/nginx/error.log | grep upstream to isolate the noise. If the error says "connection refused," your backend service isn't running or it's bound to the wrong interface (like 127.0.0.1 instead of 0.0.0.0).

How to Troubleshoot Upstream Connections

Once you’ve confirmed the error, you need to verify the path between Nginx and your application. If your app is running inside a container, you might be hitting a network isolation wall, which often requires checking Docker Compose networking to ensure the service names resolve correctly.

Use curl or netcat from the Nginx host to see if the backend responds:

Bash
# Test if the port is open
nc -zv 127.0.0.1 3000

If that returns a connection error, stop looking at Nginx. Your focus needs to shift to the application process. Check if it's running:

Bash
# Check if the process is alive
ps aux | grep node

If the process is gone, check your application logs. Sometimes the app is running but is stuck in a loop or hitting a memory limit, causing it to drop requests—leading to that dreaded 502.

Common Configuration Pitfalls

Sometimes the issue isn't that the service is down; it's that Nginx is looking in the wrong place. Check your proxy_pass directive in your server block.

SymptomLikely CauseFix
Connection RefusedApp process is downStart/Restart the service
502 Bad GatewayWrong Port/SocketUpdate proxy_pass config
Timeout/504Slow UpstreamIncrease proxy_read_timeout
Permission DeniedSocket file issuesFix Unix socket permissions

If you are using Unix sockets, make sure the user running Nginx (usually www-data or nginx) has read/write access to the .sock file. I once spent about two hours debugging a 502 only to realize the socket file was created by a different user during a manual deploy.

When the Upstream is "Too Busy"

If your logs show "upstream sent too big header" or similar, the backend might be sending data that Nginx isn't configured to handle. Try increasing the buffer size in your Nginx config:

NGINX
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

Reload Nginx with nginx -s reload and check if the error persists. If you still see the 502, it’s possible your upstream is simply overwhelmed. In that case, you might need to look into rate limiting or scaling your backend instances.

Frequently Asked Questions

What is the difference between 502 and 504 errors? A 502 means the upstream server returned an invalid response or refused the connection. A 504 means the upstream server took too long to respond, and Nginx timed out waiting for it.

Does an Nginx 502 always mean my code is broken? Not necessarily. It often means the service is down, the configuration is pointing to the wrong port, or the server has run out of resources.

How do I know if my upstream is overloaded? Check your application's internal metrics or the CPU/RAM usage of the server. If the application is struggling to process requests, it will often drop connections, resulting in a 502.

Final Thoughts

Troubleshooting an nginx 502 bad gateway is rarely about fixing Nginx itself. It's almost always about the health of the service sitting behind it. Next time you hit this, don't jump straight to changing your Nginx config. Start by verifying the backend process is actually listening. I’ve learned the hard way that assuming the service is "up" is a recipe for a long night of debugging.

Similar Posts