All Blog

HTTP 502 Bad Gateway Error: Meaning, Causes, Fixes

10 min read
Spt 9, 2026

TL;DR

TL;DR illustration

An HTTP 502 Bad Gateway error means a gateway or proxy received an invalid response from an upstream server. Visitors can retry later, but persistent errors require the website owner to inspect upstream services, proxy settings, DNS, resource usage, and logs.

Why HTTP 502 errors happen

A reverse proxy, load balancer, CDN, or API gateway accepts the request but cannot obtain a valid response from the application behind it.

Common causes include:

  • Server overload: The application runs out of CPU, memory, connections, or workers.
  • Server downtime: The application has stopped, is restarting, or is unreachable.
  • Network failure: Routing or connectivity problems interrupt communication.
  • DNS problems: The proxy resolves an outdated or incorrect upstream address.
  • Proxy misconfiguration: The gateway uses the wrong host, port, protocol, or TLS settings.
  • Application failures: Crashes, malformed headers, or closed connections produce invalid responses.
  • Firewall restrictions: Security rules block traffic between infrastructure components.

Determine whether the error affects every request, one endpoint, one region, or only peak traffic.

How to resolve an HTTP 502 error

The solution depends on whether you are visiting or operating the website.

If you are a website visitor

  1. Refresh the page once.
  2. Wait several minutes and retry.
  3. Use a private window or another browser.
  4. Switch networks or restart your router.
  5. Check the website’s status page.

Avoid repeatedly submitting checkout, booking, or payment requests. The operation may have succeeded upstream despite the error.

If you own or administer the website

  1. Confirm that the upstream service is running.
  2. Compare CDN, proxy, and application logs.
  3. Verify upstream hosts, ports, protocols, certificates, and DNS.
  4. Inspect CPU, memory, workers, connection pools, and queues.
  5. Review recent deployments and firewall rules.
  6. Capture logs before restarting failed processes.

A restart may restore service, but recurring 502 responses usually require application, capacity, or dependency fixes.

If you develop the application

Test dependencies independently. Add timeouts, health checks, circuit breakers, and structured logs with request or trace IDs. Investigate slow queries, blocked workers, and connection leaks before adding capacity.

Limit retries to safe, idempotent operations. Uncontrolled retries can amplify an outage.

Useful HTTP and network diagnostic tools

  • cURL: Inspect status codes, headers, redirects, and timing.
curl -v -I https://example.com/api/health
  • Ping: Tests reachability, although production systems may block ICMP.
  • Traceroute: Displays the network path and possible routing trouble.
  • DNS tools: Use dig or nslookup to verify resolved addresses.
  • Server logs: Review proxy, gateway, and application errors.

Test both the public gateway and upstream service. If the upstream works directly, focus on proxy configuration, TLS, DNS, or access controls.

What a 502 Bad Gateway response means

What a 502 Bad Gateway response means illustration

HTTP 502 means an intermediary received an invalid response from another server while processing a request. It does not identify the root cause, which may be overload, a crash, a broken connection, or configuration mismatch. The MDN 502 reference classifies it as a server-side error response.

The main causes behind recurring 502 responses

Recurring errors generally involve:

  1. Availability: The application is stopped or failing health checks.
  2. Capacity: Traffic exceeds workers, connections, memory, or bandwidth.
  3. Connectivity: DNS, routing, TLS, or firewall rules block communication.
  4. Response validity: The application closes the connection or sends an invalid response.

Deployment-correlated errors suggest code or configuration changes; regional failures suggest routing or DNS; traffic-correlated failures suggest capacity pressure.

Actions website visitors can take

Refresh once, try another browser or network, and check for a reported outage. If the error persists across devices, report the URL and approximate time to the website owner.

Actions website owners should take

Identify which component generated the 502, then trace the request upstream. Correlate logs, inspect health checks, and review recent changes. Under load, examine saturation before increasing timeouts, which can tie up more gateway connections.

How diagnostics reveal the failing layer

DNS checks confirm the destination, cURL exposes headers and timing, traceroute examines the route, and logs show whether the upstream accepted the request. Together, they reveal whether failure occurred at the gateway, network, or application.

How to Build an App Like This with Tencent RTC

In a calling or conferencing application, a 502 often originates from the authentication or room-management API rather than the active media session. Use Tencent RTC Engine for media while keeping identity and room authorization on your backend.

Follow the Tencent RTC integration overview to configure the SDK. Generate userSig on a trusted server, return credentials through a monitored endpoint, and never expose the secret key in client code. Retry temporary credential-service failures with bounded backoff:

async function getRoomCredentials(roomId) {
  for (let attempt = 0; attempt < 3; attempt++) {
    const response = await fetch(`/api/rtc/credentials?roomId=${roomId}`);
    if (response.ok) return response.json();
    if (response.status !== 502) throw new Error(`HTTP ${response.status}`);
    await new Promise(resolve => setTimeout(resolve, 500 * 2 ** attempt));
  }
  throw new Error("Room service temporarily unavailable.");
}

After receiving server-generated credentials, enter the room and start local media:

import TRTC from "trtc-sdk-v5";

const credentials = await getRoomCredentials("support-room-42");
const rtcClient = TRTC.create();

await rtcClient.enterRoom({
  sdkAppId: credentials.sdkAppId,
  userId: credentials.userId,
  userSig: credentials.userSig,
  roomId: credentials.roomId,
  scene: "rtc"
});

await rtcClient.startLocalAudio();
await rtcClient.startLocalVideo({ view: "local-video" });

Separating the credential API from the media path lets the client show a clear join failure without unnecessarily interrupting established audio or video sessions.

FAQ

What is HTTP 502?

HTTP 502 is a server error indicating that a gateway or proxy received an invalid response from an upstream server.

What are the most common HTTP 502 Bad Gateway causes?

Typical causes include upstream downtime, overload, DNS errors, network failures, proxy misconfiguration, invalid application responses, and firewall rules.

What is the difference between HTTP 502 and 504?

A 502 means the gateway received an invalid upstream response. A 504 Gateway Timeout means it did not receive a response within the allowed time.

Talk to us

We'd love to answer your questions and discuss how our RTC solutions can help meet your needs!
You can request demos, learn about pricing, or get personalized onboarding support. Complete the form below and one of our solution experts will be in touch shortly.
Submit
Submit that agree with the above information for contact purposes. Please read our Privacy Policy.