NGINX 502 Bad Gateway: Which Hop Failed the Request?

Illustrated infographic summarizing: NGINX 502 Bad Gateway: Which Hop Failed the Request?

By Greg Nowak. Last updated 2026-11-26.

When a 502 comes and goes, pick one failed request before raising a timeout or restarting NGINX. In a stack with Cloudflare in front of NGINX and an application behind it, follow that request from the client response to the proxy log and then to the application. You’re looking for who sent the 502 and how far the request got.

Save the timestamp with its time zone, the hostname, HTTP method, path, and any request ID the client received. Pull a successful request from the same route and time window. That gives you something to compare while the route is still serving traffic.

Did Cloudflare generate the 502?

Cloudflare can send its own 502 or pass along a 5xx from the origin. If it’s safe to repeat the request, send Accept: application/problem+json. In Cloudflare’s default structured response, cloudflare_error: true tells you Cloudflare generated the error. Keep the Ray ID and timestamp, and use the detail field, when present, to decide what to check first on the path to the origin. Custom Errors can change what comes back, so an HTML response leaves the question open. Cloudflare error responses

Look for that request in NGINX using an ID both systems recorded. If you only have the hostname, method, path, and time window, treat the match as tentative when several entries fit. If nothing turns up, confirm you’re looking at the intended origin and the right access log. Check whether buffering could delay the entry or conditional logging could skip it. NGINX logs the request under the location where processing ends, which can change after an internal redirect. NGINX HTTP log module

Read the NGINX attempts in order

$status is the response code NGINX sent. Read it alongside $upstream_addr and $upstream_status to see which backend it selected and what status it recorded for that attempt. If NGINX couldn’t select a server, $upstream_addr contains the group name and $upstream_status records 502. Start there by checking which servers in that group were eligible to handle the request. NGINX HTTP log module · NGINX upstream module

Commas separate attempts within a group. A colon separator marks a change of upstream group after an internal redirect. Match each address to the status and timings in the same position. $upstream_connect_time covers connection setup, including the TLS handshake for HTTPS backends. $upstream_header_time measures how long it took to receive the response headers, and $upstream_response_time measures the time spent receiving the upstream response. Look at the successful sample too. A retry can give the client a successful response while the log still shows that an earlier backend attempt failed. NGINX upstream module

Check what the application saw

Once you have a backend address, look for the same request in that service’s logs. An arrival entry confirms the request made it to the service. If that’s the last entry, use the matching NGINX error and any application spans to see what happened after it arrived. If the application logged a completed error response, send the service owner that record along with the matching proxy attempt and its timing.

On instrumented HTTP paths that use W3C Trace Context, OpenTelemetry carries the context in traceparent. The trace ID ties work across services together, and the span ID identifies the operation within the trace. With log correlation configured, those IDs let you jump from a log entry to its span. Use the successful request to check that the ID you plan to search for makes it through the proxy and into the application log. OpenTelemetry context propagation

Who picks up a 502 investigation, and what they check first
What you found Who starts First check
Cloudflare generated the 502 and classified it as an origin error CDN and origin operator Use the Ray ID, timestamp, and error detail to investigate the path from Cloudflare to the origin.
NGINX logged a group name in $upstream_addr and 502 in $upstream_status NGINX operator Check which backends in the group were eligible to serve the route at the time.
NGINX logged a backend address and a connection setup error NGINX and backend platform operators Match the error and timing to the attempt. Check connectivity to that backend and the TLS handshake if configured.
The matching application log shows a completed error response Application owner Open the application span and inspect any downstream calls it records.

When the November NGINX fix matters

NGINX mainline 1.31.10, released on 23 November 2026, fixed an issue that could drive up CPU use when NGINX connected to backends over TLS. If the affected route uses HTTPS upstreams, check the deployed build and compare NGINX CPU and upstream connection timings during the failure window. If the pattern fits, factor the fix into the upgrade decision and compare the same signals after rollout. NGINX mainline changelog

Set up the next on-call engineer

Set the NGINX access log format to capture the hostname, route, timestamp, correlation ID, final status, and total request time. Include the upstream addresses, statuses, and timings in attempt order, and keep that order through the log pipeline. If the pipeline only saves the last upstream status, a successful retry can hide the earlier failure from a search. NGINX HTTP log module · NGINX upstream module

Alert on the 502 rate clients see for each route and show the request count alongside it so whoever gets paged can judge the scale. Give them a view of upstream attempts to see whether failures cluster in one backend pool or route. Put the first log query in the runbook, along with the ID to carry between systems and the evidence each owner needs to take over. Greg can help piece together the Cloudflare, NGINX, and application records, then use what turns up to build the alerts and write the runbook with your team.

Related on GrN.dk

Need help with this kind of work?

Talk to Greg about tracing intermittent 502s and giving your on-call team route-level alerts and a runbook. Get in touch with Greg.

Sources

Latest articles

Trace an intermittent NGINX 502 through Cloudflare, upstream attempts, and application logs so you can see where the request failed and who should pick it up.

Find the apps, scripts and printers that still send mail through Microsoft 365 with a mailbox password, then test a replacement before the December 2026 change.

Find subscriptions and insurance expenses in e-conomic that should be spread over several months. See how AI reads the supporting documents and how your finance lead approves the proposed allocations.

Before relying on an AI project briefing, check which files it covered. A practical workflow for tracking documents, recording gaps and verifying key claims.

Check whether your contact form rejects missing or invalid Turnstile tokens, handles failed checks for genuine users and delivers accepted enquiries to your team.

Compare Bing citations, identifiable AI visits and verified enquiries to see which pages deserve attention before you increase your AI search marketing budget.

Teamtailor is phasing out Riminder on 21 November 2026. Check your parser settings, contact fields and integrations, and make sure you have a process for deleting applicant data.

WordPress jobs running late? Learn how to separate scheduling problems, task errors and a growing backlog, then choose the right scheduler or runner.

Before changing an AI workflow, check that it still does the job. Use realistic examples, clear release criteria and a comparison with the version running today.

Sending an invoice does not mean it has arrived. Understand delivery statuses, the system requirements coming in 2027, and how focused AI support can help finance teams handle errors and follow-up.