Backend Development 8 min read

Troubleshooting Intermittent 502 Errors in a Node Backend Behind Nginx Reverse Proxy

The article details a step‑by‑step investigation of intermittent 502 errors when routing client requests through Nginx to a Node.js service, identifies malformed URL characters triggering Node's clientError event as the root cause, and presents code and configuration fixes to prevent the issue.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Troubleshooting Intermittent 502 Errors in a Node Backend Behind Nginx Reverse Proxy

Background: The department migrated Java web interfaces to Node, naming the service "HOTDOG". A Node service was set up as a bridge between clients and the web interface, with Nginx as reverse proxy and load balancing.

Problem: After deployment, requests to the new Node API intermittently returned 502 errors. Direct access to the Node server (IP+PORT) worked, indicating the issue lay in the proxy layer.

Investigation steps:

Checked Nginx logs – suggested half‑open TCP connections causing premature closure.

Examined Node logs – no 502 entries, low system load, confirming Nginx was not receiving a response.

Tried long‑lived upstream connections – no improvement.

Verified Node’s default HTTP timeout (2 min) was not the cause.

Captured packets with tcpdump – observed Nginx sending a request, the Node process ACK‑ing then sending FIN, resulting in Nginx returning 502.

Reproduced the issue with a minimal Node server handling a request containing non‑ASCII characters, which triggered the clientError event and caused the socket to hang.

Solution:

Add a clientError handler in the Node server to log the malformed request and respond with HTTP/1.1 400 Bad Request , preventing the socket from hanging.

Configure Nginx (or OpenResty) to treat each request independently and avoid fail‑timeout cascading failures.

Conclusion: The 502 errors were caused by Node rejecting requests with illegal characters at the TCP level, which Nginx interpreted as a server failure. Proper error handling in Node and refined Nginx upstream settings resolve the issue and improve overall reliability.

Node.jstroubleshootingnginxreverse-proxy502clientError
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.