How to Diagnose a 502 Bad Gateway Error

Ryan
Ryan
IP Proxy Research Team

A 502 Bad Gateway error means that one server in the request path could not get a valid response from another server upstream.

Your browser, API client, or application may have successfully reached a CDN, load balancer, reverse proxy, or gateway. However, that intermediary could not complete the next part of the connection.

This is normally a server-side or gateway-side problem. If the error appears only in one browser, network, proxy route, or application, the client’s network configuration may still be relevant—but it should not be the first assumption.

Direct Answer

HTTP 502 Bad Gateway means a server acting as a gateway or proxy received an invalid response from an upstream server. Start by identifying which intermediary returned the error. Then check the upstream service, connection, protocol, DNS resolution, firewall rules, and recent deployment changes.

Key Takeaways
  • 502 is a server error in the gateway-to-upstream connection.
  • The word “proxy” often refers to a CDN, load balancer, or reverse proxy—not necessarily a user-configured proxy IP.
  • Common causes include an unavailable backend, refused connection, invalid upstream response, TLS mismatch, DNS failure, or incorrect gateway configuration.
  • A 502 differs from a 504: a 502 involves an invalid or failed upstream response, while a 504 involves an upstream timeout.
  • If the error occurs only through one proxy route, test that route separately before blaming the target website.
  • Automated workflows should use limited retries and preserve diagnostic evidence.
Table of Contents

What Does 502 Bad Gateway Mean?

The HTTP specification defines 502 Bad Gateway as a response sent when a server acting as a gateway or proxy receives an invalid response from an upstream server.

You can review the formal definition in the HTTP 502 specification or the more accessible MDN 502 Bad Gateway reference.

The browser usually cannot see the exact upstream failure. It only receives the 502 response generated by the intermediary.

For example, the browser may reach a CDN successfully. The CDN then attempts to connect to the website’s origin server. If the origin sends an unusable response, closes the connection unexpectedly, or cannot be reached correctly, the CDN may return 502 to the browser.

A 502 specifically points to a failed or invalid response between a gateway and an upstream server. This differs from a 504, where the upstream response did not arrive within the required time.

A 502 Does Not Always Come From the Website You Opened

The response may be generated by a CDN, reverse proxy, load balancer, API gateway, corporate gateway, or user-configured forward proxy. The first diagnostic task is identifying which intermediary produced it.

Where Can a 502 Occur?

A modern request may pass through several systems before it reaches the application that generates the content.

How a 502 Bad Gateway error occurs between a client, gateway, and upstream server
Figure 1: A 502 error occurs when a gateway cannot obtain a valid response from the upstream server.
Browser or API Client Forward Proxy or VPN
Optional
CDN or Load Balancer Reverse Proxy or Gateway Upstream Application

A 502 may be generated whenever one intermediary receives an invalid response from the next system in the chain.

This distinction matters because the word “proxy” has two common meanings:

  • Forward proxy: configured by the user or application to send outbound traffic.
  • Reverse proxy: operated in front of a website or application to receive incoming traffic and pass it to backend servers.

Most public 502 pages are generated by server-side infrastructure such as a CDN, reverse proxy, or load balancer. A user-configured forward proxy can also generate 502, but that is a different failure layer.

Common Causes of 502 Bad Gateway

The status code identifies the failing relationship between a gateway and an upstream service. It does not identify the exact configuration, server, or connection that failed.

Common causes of 502 errors including upstream failure, DNS problems, protocol mismatch, firewall blocks, and gateway configuration
Figure 2: Common causes of 502 errors include upstream outages, DNS failures, protocol mismatches, firewall rules, and gateway configuration problems.
Table 1: Common causes of 502 Bad Gateway errors and the evidence to check for each cause.
Cause What May Be Happening What to Check
Upstream application is unavailable The gateway is running, but the backend process is stopped, unhealthy, or restarting. Application health, process status, container status, deployment logs, and health checks.
Connection refused or reset The gateway reaches the backend address, but the connection is rejected or closed unexpectedly. Backend listening port, process logs, firewall rules, connection resets, and network policy.
Incorrect upstream host or port The gateway is configured to contact the wrong service, address, container, or port. Reverse-proxy configuration, service discovery, container mapping, and load-balancer targets.
Invalid upstream response The backend returns malformed headers, incomplete data, or closes the connection before completing the response. Gateway error logs, application output, response size, header format, and backend exceptions.
TLS or protocol mismatch The gateway expects HTTPS while the backend serves HTTP, or certificate and SNI settings do not match. HTTP or HTTPS scheme, certificate validity, SNI, supported TLS versions, and upstream protocol.
Gateway-to-upstream DNS failure The gateway cannot resolve the backend hostname or resolves it to an outdated address. DNS resolution from the gateway environment, service records, resolver cache, and recent DNS changes.
Firewall or allowlist rule The origin rejects or drops connections from the CDN, load balancer, or gateway. Security groups, firewall logs, origin allowlists, provider IP ranges, and blocked ports.
Overloaded or unstable backend The application crashes, rejects connections, or returns incomplete responses during traffic spikes. CPU, memory, worker count, queue depth, connection limits, and restart events.
User-configured proxy route failure A forward proxy cannot connect to the destination or returns its own gateway error. Proxy endpoint, authentication, protocol, DNS behavior, route health, and provider response body.

What Website Visitors Can Check

A normal website visitor usually cannot repair the origin server, reverse proxy, or load balancer. The goal is to determine whether the issue is temporary or limited to one environment.

  1. Refresh the page once. A temporary backend restart or gateway problem may already have cleared.
  2. Open another page on the same site. This helps determine whether one endpoint or the entire service is affected.
  3. Check the service’s status page. Look for a reported outage or maintenance event.
  4. Try another browser or private window. This can rule out an unusual extension, cached error page, or browser-specific configuration.
  5. Test another network. If the service works elsewhere, inspect the original network, DNS, VPN, firewall, or proxy configuration.
  6. Disable a custom proxy or VPN temporarily for diagnosis. Do this only to compare the request path.

If the same site returns 502 across several devices and networks, the issue is probably controlled by the website, hosting provider, CDN, or upstream service.

How Developers and Website Owners Should Diagnose 502

Website owners have access to stronger evidence than visitors. Start by identifying the system that generated the response, then trace the next upstream connection.

Five-step process for diagnosing a 502 error by identifying the intermediary, checking upstream health, verifying DNS, reviewing logs, and comparing proxy routes
Figure 3: A structured 502 diagnosis starts with the intermediary and upstream health before moving to DNS, logs, and route comparison.
Identify the Response Source
  • Inspect the error-page branding.
  • Check the Server and Via headers.
  • Look for CDN, gateway, or request-ID headers.
  • Check Proxy-Status when it is present.
Check the Next Upstream
  • Confirm that the backend process is running.
  • Verify the host, port, protocol, and health check.
  • Test DNS resolution from the gateway.
  • Review connection and application logs.

Identify Which Layer Returned the Error

Inspect the response headers and body. A branded CDN page suggests that the CDN generated the response. An NGINX page may come from the origin’s reverse proxy, a hosting layer, or another intermediary.

If the response contains a Proxy-Status header, it may provide additional information about how an intermediary handled the request. The header is defined in RFC 9209, but it is not included by every service.

Check Upstream Health

Confirm that the backend process is running and accepting connections on the expected address and port. Check service discovery, container health, load-balancer targets, and recent restarts.

Review Gateway and Application Logs Together

Match the request timestamp and request ID across the gateway and upstream application logs.

Look for messages such as:

  • Connection refused.
  • No healthy upstream available.
  • Connection reset by peer.
  • Upstream closed the connection prematurely.
  • Invalid response header.
  • TLS handshake failure.
  • Name resolution failure.

Verify the Upstream Protocol

Check whether the gateway is connecting to the backend over the correct HTTP or HTTPS scheme. Also verify the hostname, SNI setting, certificate trust, port, and supported protocol version.

Check Recent Changes

Review deployments, reverse-proxy edits, DNS updates, certificate renewals, firewall changes, scaling events, container updates, and backend restarts that occurred shortly before the error began.

Compare Edge and Origin Results

When your CDN or hosting platform exposes separate edge and origin status information, compare them. This can show whether the origin returned an error or the edge generated the 502 while trying to contact it.

Cloudflare’s official 502 and 504 troubleshooting documentation is one example of provider-specific guidance for separating edge and origin failures.

How Proxy Settings Relate to 502

The word “proxy” in the 502 definition does not automatically refer to the proxy IP configured in your browser, crawler, or automation tool.

There are two separate situations.

Table 2: How server-side reverse proxies and user-configured forward proxies relate to 502 errors.
Proxy Type Role How It May Produce 502 What to Check
Reverse proxy, CDN, or load balancer Sits in front of the website or API. Cannot get a valid response from the origin or backend service. Origin health, upstream logs, gateway settings, TLS, DNS, firewall, and backend response.
Forward proxy configured by the user Sends outbound traffic for a browser, script, or application. Cannot connect to the target, cannot complete the tunnel, or generates its own gateway error. Proxy endpoint, port, protocol, authentication, route health, DNS, and provider response.

If a request works directly but returns 502 through a proxy, test the proxy independently. Confirm that the proxy is active, supports the target protocol, and can reach a simple public URL.

The guide on how to check whether a proxy is working explains how to verify the visible IP and confirm that traffic is using the intended route.

Do Not Confuse 502 with Proxy Authentication Failure

Incorrect proxy credentials normally produce 407 Proxy Authentication Required. A 502 more often means that a gateway or proxy could not complete its upstream connection or received an unusable response.

Changing the proxy cannot repair a failed origin deployment, broken reverse-proxy configuration, invalid backend response, or unavailable upstream server.

If repeated API or public web data tests require a stable, approved network route, a static residential proxy can keep the visible IP and connection path consistent while you compare responses across sessions. This can improve test consistency, but it cannot fix an unavailable upstream server or broken gateway configuration.

How to Inspect a 502 Response with curl

Use curl to inspect the status code, response headers, body, redirects, and connection details.

Inspect the Response Headers and Body

curl -i -L \
  "https://example.com/"

Review:

  • The final status code.
  • The final URL after redirects.
  • The Server, Via, and Proxy-Status headers when present.
  • CDN or gateway request IDs.
  • The error-page content.

Inspect Connection Details

curl -v -L \
  --connect-timeout 10 \
  --max-time 30 \
  "https://example.com/"

Verbose mode can show DNS resolution, the destination address, TLS negotiation, proxy tunneling, redirects, and response headers.

Compare the Request Through a Proxy

curl -i \
  --proxy "http://proxy.example.com:8000" \
  --proxy-user "PROXY_USER:PROXY_PASSWORD" \
  "https://example.com/"

If the direct request succeeds and the proxied request returns 502, inspect the proxy route, protocol, endpoint, and provider response. Keep the target URL, headers, and request method unchanged during the comparison.

Command output can contain IP addresses, cookies, authorization headers, proxy passwords, and request IDs. Remove sensitive information before sharing logs.

502 Errors in API and Web Data Workflows

Automated workflows should classify 502 responses instead of retrying them indefinitely.

Store the following information:

  • Request URL and method.
  • Timestamp and retry number.
  • Status code and a limited response sample.
  • Relevant response headers and request ID.
  • Proxy endpoint or route identifier when used.
  • Final URL and redirect chain.
  • Connection and timeout details.

Use Limited Retries

An occasional 502 may be temporary, especially during a backend restart or short infrastructure failure. A retry can be reasonable, but it should be limited.

Use exponential backoff with jitter rather than immediate repeated requests. Retry safe and idempotent operations such as GET and HEAD first. Be careful when retrying requests that create, charge, submit, or modify data unless the API supports an idempotency mechanism.

Practical Retry Rule

Retry a small number of times with increasing delays. If the same route continues returning 502, pause the workflow and preserve the response evidence. Repeatedly switching IPs or sending requests faster can hide the original failure.

Compare Routes Without Changing Other Variables

If the error appears only through one proxy endpoint, region, or environment, keep the URL, method, headers, account, and request body unchanged while switching only the route.

If all routes return the same 502 response, the target-side gateway or upstream service is more likely responsible.

Common Troubleshooting Mistakes

Table 3: Common mistakes that make 502 Bad Gateway troubleshooting less effective.
Mistake Why It Causes Problems Better Approach
Assuming every 502 is caused by the user’s proxy IP The error may come from the website’s CDN, load balancer, reverse proxy, or origin. Identify which intermediary generated the response first.
Refreshing or retrying without limits A persistent upstream failure will continue returning the same result. Use limited retries and preserve logs.
Changing the proxy, headers, account, and URL together The test cannot show which variable affected the result. Change one variable at a time.
Checking only the browser error page The page may hide the source and detailed failure reason. Inspect headers, request IDs, gateway logs, and upstream logs.
Treating 502 and 504 as the same error They point to different gateway-to-upstream failures. Check whether the response was invalid or did not arrive in time.
Clearing browser data before checking service health A genuine server-side 502 will not be fixed by deleting cookies. Check whether the error affects other users and networks first.

Frequently Asked Questions

What does 502 Bad Gateway mean?
It means a server acting as a gateway or proxy received an invalid response from an upstream server.
Is 502 a client error or a server error?
502 belongs to the 5xx server-error class. It normally reflects a failure between a gateway and an upstream server, although a custom client proxy or network route may generate the response in some environments.
Can refreshing fix a 502 error?
It may help when the issue is temporary, such as a short backend restart. It will not fix a persistent upstream outage, invalid response, or gateway configuration problem.
What is the difference between 502 and 504?
A 502 means the gateway received an invalid or failed upstream response. A 504 means the gateway did not receive an upstream response within the required time.
Can a proxy IP cause a 502 Bad Gateway error?
A user-configured proxy can return 502 when it cannot complete the upstream connection. However, many 502 errors come from the target website’s CDN, load balancer, reverse proxy, or origin infrastructure rather than the user’s proxy IP.
Why do I get 502 only when using a proxy?
The proxy endpoint may be unavailable, use the wrong protocol, fail to connect to the target, or generate its own gateway response. Compare the same request without the proxy and inspect the proxy response headers and body.
Does changing DNS fix 502 Bad Gateway?
Usually not for a public server-side 502. DNS matters when the gateway cannot resolve its upstream hostname or when the error occurs only in one custom network environment. Confirm the failing layer before changing DNS.
Should a crawler automatically retry 502 responses?
A limited retry may be appropriate for temporary failures, especially for safe requests such as GET. Use increasing delays, cap the retry count, and avoid repeating non-idempotent operations without protection.
How can a website owner find the source of a 502?
Inspect the response headers and request ID, identify the gateway that returned the error, and match the request across gateway, CDN, load-balancer, and upstream application logs.

Final Thoughts

A 502 Bad Gateway error tells you that an intermediary could not obtain a valid response from the next upstream system. It does not identify the exact server or configuration that failed.

Website visitors should first determine whether the issue affects other networks and users. Developers and administrators should identify the gateway, inspect response headers, check upstream health, and compare gateway and application logs.

If the error appears only through a proxy, test that route separately while keeping the target URL, method, and request headers consistent. A different IP cannot repair a broken origin server or reverse-proxy configuration.

About the author
View all articles
Ryan
Ryan
IP Proxy Research Team

Ryan is a web data and proxy infrastructure specialist focused on IP networks, scraping systems, SERP APIs, and global data access solutions. He shares practical insights on proxy usage, data collection architecture, and scalable web intelligence systems.

Service areas
Proxy IP Web Scraping & Data Infrastructure Specialist

You may be interested in

HTTP 503 Service Unavailable troubleshooting cover showing temporary server overload

HTTP 503 Service Unavailable: Causes and Troubleshooting

An HTTP 503 Service Unavailable error means the server is temporarily not ready to handle the request. The website, API, CDN, load balancer, or upstream service may be overloaded, under maintenance, restarting, or intentionally reducing new work until it recovers. For a normal visitor, a 503 often appears as a temporary error page. Developers, QA teams, proxy users, and public web data workflows should treat it as an availability signal—not as a valid page response and not as immediate proof that the browser, crawler, or proxy is broken. Direct Answer HTTP 503 Service Unavailable is a server error status code...

Ryan

Ryan

IP Proxy Research Team

401 vs 403 comparison showing authentication failure and permission refusal

How to Diagnose 401 and 403 Errors

401 Unauthorized and 403 Forbidden are both HTTP client error responses, but they point to different parts of the access process. A 401 response usually means the website or API does not accept your current authentication credentials. A 403 response means the request was understood, but the server refuses to allow the requested action. If a proxy is involved, there is one more status to check: 407 Proxy Authentication Required. It means the proxy itself needs valid credentials before it can forward the request. Direct Answer 401 usually means “check your login, token, API key, or authorization header.” 403 usually...

Ryan

Ryan

IP Proxy Research Team

How to Diagnose and Fix a 403 Forbidden Error

How to Diagnose and Fix a 403 Forbidden Error

A 403 Forbidden error means the server received and understood the request but decided not to complete it. The page, API endpoint, file, or action may exist, but the current account, request, or network context is not allowed to access it. This does not automatically mean the website is broken. It also does not prove that the IP address is the problem. A 403 can come from account permissions, application rules, server configuration, CDN or firewall policies, request headers, session state, network restrictions, or website access policies. The correct fix depends on which system returned the refusal. Refreshing the page,...

Ryan

Ryan

IP Proxy Research Team

Ready to scale your data operations?
Join 10,000+ teams using IPWeb to power their web data collection. Start free today.

Strictly anti-abuse

Fraud, automated operation, and unauthorized use are prohibited.

Enterprise-level services

For legitimate commercial and technical use cases only

Risk control and restrictions

Abnormal behavior may trigger service restrictions or termination.

Compliance data use

Data acquisition and use must comply with relevant regulations.

Privacy protection first

The collection or misuse of sensitive personal information is strictly prohibited.

All services are subject to《the Usage Policy》