What Does 502 Bad Gateway Mean?
A 502 Bad Gateway response means that a server acting as a gateway or proxy received an invalid response from an upstream server while attempting to fulfill the request.
A 502 response confirms several important facts:
- An intermediary was involved: A CDN, reverse proxy, gateway or load balancer handled the request.
- An upstream system was contacted: The intermediary attempted to obtain content from another server or service.
- The response was unusable: The upstream connection or response did not satisfy the intermediary’s expectations.
- The requested content was not delivered: The client received the gateway error instead of the intended resource.
- The root cause is not identified by the status alone: Logs from both the gateway and upstream system are still required.
The term “bad gateway” does not necessarily mean the gateway itself is defective. The gateway may be correctly reporting a malformed, interrupted or otherwise invalid upstream response.
How Does an HTTP 502 Response Work?
An HTTP 502 response occurs when an intermediary accepts a client request, forwards it upstream and cannot use the response returned by the upstream system.
- A browser or crawler requests a URL.
- The request reaches a CDN, gateway, proxy or load balancer.
- The intermediary selects an upstream server.
- The request is forwarded to that upstream system.
- The upstream connection or response is invalid, interrupted or unusable.
- The intermediary returns
502 Bad Gatewayto the client.
Browser or crawler
→ CDN or reverse proxy
→ Upstream application
→ Invalid upstream response
→ Gateway returns HTTP 502
The client normally cannot see the original upstream exchange. It sees only the response generated by the intermediary that handled the failure.
What Is a Gateway or Reverse Proxy?
A gateway or reverse proxy is an intermediary that receives client requests and forwards them to one or more upstream servers that generate the actual response.
These intermediaries can perform several functions:
- TLS termination
- Load balancing
- Caching
- Compression
- Authentication and access control
- Web application firewall filtering
- Routing requests to different applications
- Hiding internal origin addresses
- Serving static or fallback responses
Public client
→ Reverse proxy
→ Application server A
→ Application server B
→ API or database dependency
The client may never communicate directly with the origin application. The proxy controls the public connection and translates upstream success or failure into the response sent to the client.
What Causes a 502 Bad Gateway Error?
A 502 can be caused by an unavailable upstream process, malformed response, broken proxy configuration, TLS failure, connection reset, invalid headers or incompatible communication between serving layers.
| Cause | Example | Evidence to inspect |
|---|---|---|
| Upstream process is down | Nginx forwards to PHP-FPM, but the PHP-FPM service is stopped. | Service status, socket availability and upstream logs |
| Wrong host or port | The proxy connects to a port where no valid service is listening. | Proxy configuration and network connection tests |
| Connection reset | The upstream application closes the connection before completing the response. | Gateway errors, upstream crashes and process logs |
| Malformed HTTP response | The upstream returns invalid headers or protocol output. | Raw upstream response and application logs |
| TLS handshake failure | The proxy cannot establish a trusted encrypted connection to the origin. | Certificate chain, hostname, cipher and TLS logs |
| Header limits exceeded | The upstream returns headers larger than the proxy accepts. | Header size, cookie size and proxy buffer logs |
| Protocol mismatch | The gateway expects HTTP while the upstream expects HTTPS. | Upstream scheme and port configuration |
| Container or socket failure | The proxy points to a stale Unix socket or unavailable container. | Container health, socket ownership and orchestration events |
| Origin firewall rule | The origin rejects connections from the CDN or load balancer. | Firewall logs, allowlists and source addresses |
Multiple causes can produce the same public response. A reliable diagnosis must identify which upstream exchange failed and why.
502 Error vs 500 Internal Server Error
A 502 identifies a failed gateway-to-upstream exchange. A 500 identifies an unexpected internal failure within the server processing the request.
| Dimension | 502 Bad Gateway | 500 Internal Server Error |
|---|---|---|
| Architecture | Requires a gateway or proxy relationship | Can occur within one server or application |
| Failure meaning | Invalid upstream response | Unexpected internal processing failure |
| Typical source | CDN, reverse proxy or load balancer | Application, runtime or server configuration |
| Primary logs | Gateway and upstream logs | Application and server logs |
| Diagnostic question | Why was the upstream response invalid? | What failed while processing the request? |
An application may return 500 to a proxy, which then passes that 500 through unchanged. A true 502 normally means the proxy could not use the upstream exchange itself.
Review generic server failures in What Is a 500 Internal Server Error?.
502 Error vs 503 Service Unavailable
A 502 means a gateway received an invalid upstream response. A 503 means the service is temporarily unable to handle the request.
| Dimension | 502 Bad Gateway | 503 Service Unavailable |
|---|---|---|
| Primary condition | Invalid response from upstream | Temporary lack of service availability or capacity |
| Common source | Gateway, proxy, CDN or load balancer | Application, server, CDN or load balancer |
| Typical example | PHP-FPM closes the connection unexpectedly | All workers are deliberately unavailable during maintenance |
| Retry-After | Not normally central to the meaning | Can communicate a recovery estimate |
| Corrective focus | Repair the upstream exchange | Restore service readiness or capacity |
Review controlled temporary outages in What Is a 503 Service Unavailable Error?.
502 Error vs 504 Gateway Timeout
A 502 means the gateway received an invalid upstream response. A 504 means the gateway did not receive the required upstream response within its allowed time.
| Dimension | 502 Bad Gateway | 504 Gateway Timeout |
|---|---|---|
| Upstream outcome | A response or connection was invalid | The response did not arrive in time |
| Typical example | Upstream closes the connection prematurely | Upstream processing exceeds the proxy timeout |
| Primary evidence | Protocol, connection and response-format errors | Latency traces and timeout events |
| Corrective focus | Repair connection validity or response format | Reduce upstream latency or set justified timeouts |
| Architecture requirement | Gateway or proxy relationship | Gateway or proxy relationship |
A slow upstream service can sometimes produce different errors at different layers. Diagnose the exact gateway log message instead of assuming every delayed response is a 504.
502 Error vs 520 Web Server Returned an Unknown Error
HTTP 502 is a standardized server-error status. A 520 response is a nonstandard CDN-specific status used when an edge network receives an unexpected or unclassifiable origin response.
| Dimension | 502 Bad Gateway | 520 Unknown Error |
|---|---|---|
| Standard status | Yes | No |
| Meaning | Invalid response from an upstream server | CDN received an unexpected origin response it could not classify normally |
| Common source | Any standards-compliant gateway or proxy | Specific CDN implementation |
| Diagnostic evidence | Gateway and upstream logs | CDN event details, edge identifiers and origin logs |
| Corrective focus | Repair the upstream exchange | Identify the origin behavior that the CDN could not interpret |
Do not assume a nonstandard CDN code has identical semantics across providers. Use the provider’s current documentation and incident identifiers when diagnosing it.
Which Server Actually Returns the 502 Error?
The gateway or proxy that cannot use the upstream response normally returns the public 502 error. The upstream server may have failed, but it does not necessarily generate the 502 response itself.
Identify the responding layer by checking:
- Response headers: Server, Via, CDN, request ID and proxy-specific headers can provide clues.
- Error-page branding: A CDN or hosting platform may identify itself visually.
- Gateway access logs: These show the public request and selected upstream.
- Upstream logs: These confirm whether the request arrived and how processing ended.
- Direct-origin testing: A controlled authorized request can show whether the origin works without the intermediary.
- Regional comparison: Different edge locations may produce different results.
A missing upstream log entry can indicate that the request never reached the application. It can also indicate incomplete logging, so absence alone is not conclusive.
Can a CDN Cause a 502 Bad Gateway Error?
Yes. A CDN can return 502 when it cannot obtain or validate a usable response from the configured origin server.
CDN-related causes include:
- The origin address or port is incorrect.
- The origin rejects CDN source addresses.
- The origin certificate is invalid for the configured hostname.
- The TLS mode is incompatible with the origin.
- The origin closes connections unexpectedly.
- An edge function changes the request incorrectly.
- The CDN sends a Host header the origin does not recognize.
- The origin response exceeds header or protocol limits.
- One CDN region cannot reach the origin reliably.
Compare the public CDN response with a safe, authorized direct-origin test. Also review CDN event identifiers and the origin logs from the same timestamp.
Can Nginx Cause a 502 Bad Gateway Error?
Nginx can return 502 when it is configured as a reverse proxy or FastCGI gateway and cannot obtain a valid response from its upstream target.
Common Nginx-related causes include:
- The upstream service is not running.
proxy_passpoints to the wrong host, scheme or port.fastcgi_passpoints to a missing PHP-FPM socket.- The Nginx worker cannot access the Unix socket.
- The upstream closes the connection prematurely.
- The upstream sends invalid headers.
- The Host header or SNI name is wrong.
- Proxy buffers are insufficient for the returned headers.
- A container hostname no longer resolves.
Start with the Nginx error log, because messages such as “connection refused,” “upstream prematurely closed connection” or “no live upstreams” identify different failure paths.
Can Apache Cause a 502 Bad Gateway Error?
Apache can return 502 when modules such as mod_proxy or mod_proxy_fcgi cannot obtain a valid response from the configured backend.
Potential causes include:
- The proxied application is unavailable.
- The backend hostname or port is incorrect.
- The PHP-FPM socket is missing or inaccessible.
- The backend terminates the connection unexpectedly.
- A proxy rule sends the request to the wrong protocol.
- The upstream TLS certificate fails validation.
- Worker or connection limits are exhausted.
- A deployment changes the backend path without updating Apache.
Review Apache’s error log, virtual-host configuration and backend-service logs from the same request window. Confirm which proxy module handled the route.
Can PHP-FPM Cause a 502 Error?
Yes. Nginx or Apache can return 502 when PHP-FPM is stopped, unreachable, overloaded or terminates a request before returning a valid FastCGI response.
Common PHP-FPM failure conditions include:
- The PHP-FPM service is not running.
- The configured socket path does not match.
- The web-server user cannot access the socket.
- All PHP-FPM workers are occupied or unavailable.
- A PHP process crashes during execution.
- The process is killed for exceeding memory or resource limits.
- A deployment uses an incompatible PHP extension.
- The pool configuration cannot support current demand.
Inspect the PHP-FPM service state, pool logs, slow log, process limits, socket ownership and matching web-server error. A socket existing on disk does not prove the service behind it is healthy.
Can a Load Balancer Cause a 502 Error?
Yes. A load balancer can return 502 when a selected backend closes the connection, returns malformed output or fails the protocol exchange expected by the load balancer.
Potential causes include:
- A backend accepts the connection but resets it.
- The health check passes while the real application route fails.
- The target group uses the wrong protocol or port.
- One backend instance runs an incompatible release.
- The application response contains invalid headers.
- TLS settings differ between the load balancer and targets.
- Connection-draining or deployment behavior closes active requests.
- Container readiness is reported before the application is ready.
Compare error rates by backend target, zone, release and request path. A single unhealthy instance can produce intermittent 502 errors while most requests succeed.
Can DNS Cause a 502 Bad Gateway Error?
DNS can contribute to a 502 when a gateway resolves an upstream hostname to the wrong, stale or unreachable address and then fails to obtain a valid response.
DNS-related scenarios include:
- An upstream hostname points to an old server.
- A container or service record changes but the proxy retains a stale address.
- Split-horizon DNS returns the wrong internal destination.
- A regional resolver returns inconsistent records.
- The gateway cannot resolve the upstream hostname.
- An IPv6 address is returned for an upstream that is not reachable over IPv6.
- A recent migration has not propagated through all serving layers.
The user-facing domain can resolve correctly while the gateway’s private upstream hostname fails. Test DNS from the same network and environment where the gateway runs.
Can SSL or TLS Cause a 502 Error?
Yes. A gateway can return 502 when it cannot complete or trust the TLS connection to an HTTPS upstream server.
Possible TLS causes include:
- The origin certificate has expired.
- The certificate does not match the upstream hostname.
- The certificate chain is incomplete.
- The gateway does not trust the issuing certificate authority.
- The proxy sends the wrong SNI hostname.
- The origin and gateway share no supported protocol or cipher.
- Mutual TLS credentials are missing or invalid.
- The configured upstream scheme uses HTTPS on an HTTP-only port.
The public browser-to-CDN certificate can be valid while the private CDN-to-origin certificate fails. Both TLS connections must be evaluated separately.
Are 502 Errors Bad for SEO?
An isolated short-lived 502 does not automatically damage rankings, but repeated or prolonged 502 errors can block crawling, reduce server trust signals and eventually affect indexing.
SEO risk increases when 502 errors affect:
- The homepage or major category pages
- Large groups of canonical URLs
- New pages awaiting discovery
- XML sitemap URLs
- JavaScript, CSS or rendering APIs
- Mobile requests or specific regions
- Important product and conversion paths
- Googlebot more frequently than normal users
A 502 is normally treated as a temporary server failure, but the status does not protect inaccessible content indefinitely.
How Does Google Handle 502 Responses?
Googlebot treats a 502 as a server error, cannot retrieve the intended page from that request and may retry the URL later.
- Googlebot requests the URL.
- A gateway returns HTTP 502.
- Google does not receive the intended page content.
- The failure contributes to the site’s observed server health.
- Googlebot may revisit the URL later.
- Significant or persistent 5xx failures can reduce crawling.
- Prolonged unavailability can eventually affect indexing.
Google does not publish a fixed number of 502 responses that guarantees a ranking, crawling or indexing change.
See Google Search Central’s crawling-error guidance.
Can 502 Errors Reduce Googlebot Crawling?
Yes. A significant number of 502 and other 5xx errors can signal that the serving infrastructure is unhealthy, causing Googlebot to reduce crawl activity.
Crawl reduction becomes more likely when:
- Many URLs return 502 during the same period.
- The same URL patterns fail repeatedly.
- Gateway latency increases before failures occur.
- Connection resets or timeouts accompany the 502 responses.
- Robots.txt or XML sitemaps also fail.
- Failures occur across multiple edge regions.
- Successful responses do not return between crawler visits.
Googlebot backs off to avoid increasing pressure on struggling infrastructure. Crawl activity can recover after stable responses return.
Can a Long 502 Outage Remove Pages From Google?
Yes. When Google repeatedly cannot retrieve a URL because of prolonged 502 responses, the page can eventually lose indexing and Search visibility.
The risk depends on:
- The length of the outage
- The frequency of Googlebot requests
- The percentage of the site affected
- Whether failures are continuous or intermittent
- Whether the URL was previously indexed
- Whether successful responses return after recovery
- Whether critical internal signals still point to the page
A correct 502 response is preferable to disguising the error as successful content, but it does not preserve inaccessible URLs indefinitely.
Should a 502 Error Page Return 200?
No. A page displayed because the gateway could not retrieve the requested resource should return an accurate 5xx status rather than 200 OK.
| Response | What it communicates | Result |
|---|---|---|
502 Bad Gateway |
The gateway received an invalid upstream response. | Accurate for a genuine gateway failure |
200 OK |
The requested resource was served successfully. | Misleading when only an error message is shown |
503 Service Unavailable |
The service is temporarily unavailable. | Appropriate only when that condition is the accurate cause |
A branded fallback page can help users navigate, but visual design does not replace the correct HTTP response.
Should You Redirect a 502 Error to the Homepage?
No. Redirecting a gateway failure to the homepage does not repair the upstream connection and misrepresents the requested resource.
A homepage redirect can:
- Hide the original 502 from monitoring
- Change the requested URL unexpectedly
- Prevent engineers from reproducing the failure
- Create irrelevant redirect behavior
- Confuse users who expected a specific resource
- Cause crawlers to process an unrelated destination
- Distort analytics and incident measurements
Keep the requested URL and return the correct error response until the gateway-to-upstream exchange is restored.
How Do You Find 502 Errors?
Find 502 errors by combining live status checks, uptime monitoring, crawl data, CDN logs, proxy logs, load-balancer metrics and upstream application evidence.
| Evidence source | What it reveals | Limitation |
|---|---|---|
| HTTP status checker | The current public response and headers | May miss intermittent failures |
| Website crawl | URLs returning 502 during the crawl | Depends on timing and URL discovery |
| Uptime monitoring | Availability history and regional differences | Usually checks selected endpoints |
| CDN or proxy logs | Selected upstream, edge region and gateway error | Requires correct retention and identifiers |
| Load-balancer metrics | Error rate by target, zone and backend | May not show application root cause |
| Upstream logs | Whether the request arrived and how processing ended | May be absent when the gateway never connected |
| Search Console | Server failures observed during Google crawling | Not a complete real-time incident monitor |
Group failures by route, upstream target, region, deployment version, user agent and time window. Intermittent 502 errors are often linked to one instance or one edge path.
Check a live response with the HTTP Status & Redirect Chain Tracer.
How Do You Diagnose a 502 Bad Gateway Error?
Diagnose a 502 by identifying the responding gateway, the selected upstream target and the exact reason the upstream exchange was considered invalid.
- Capture the exact URL, method, timestamp and response headers.
- Determine whether the failure is constant or intermittent.
- Identify the CDN, proxy or load balancer returning the error.
- Record the gateway request ID or trace ID.
- Find the selected upstream host, port and protocol.
- Check whether the request reached the upstream service.
- Review gateway and upstream logs together.
- Check service health, sockets, containers and target status.
- Validate DNS, TLS, SNI and Host-header configuration.
- Compare recent deployments and configuration changes.
- Test a controlled direct-upstream request where safe.
- Verify the fix across routes, regions and repeated requests.
The most useful diagnostic message is often in the gateway error log rather than the public response page.
How Do You Fix a 502 Bad Gateway Error?
Fix a 502 by restoring a valid gateway-to-upstream exchange, then verify stable responses across the affected routes, targets and regions.
| Verified cause | Possible correction | Required verification |
|---|---|---|
| Upstream service stopped | Restore the service and investigate why it stopped | Gateway connects and receives valid responses |
| Wrong host, port or scheme | Correct the upstream configuration | Connection reaches the intended service securely |
| PHP-FPM socket unavailable | Restore PHP-FPM and correct socket path or ownership | Dynamic PHP routes return expected content |
| TLS validation failure | Correct certificate, hostname, trust chain or SNI | Gateway completes the upstream TLS handshake |
| Malformed upstream headers | Fix application output or justified proxy limits | Raw response conforms to HTTP requirements |
| One unhealthy backend | Remove or repair the target and improve health checks | All active targets serve valid responses |
| Connection reset under load | Repair crashes, capacity, worker limits or deployment behavior | Load tests complete without resets |
| Firewall rejection | Allow authorized gateway addresses with narrow rules | Gateway connects without broad public exposure |
After recovery, recrawl important URLs, inspect 5xx rates, compare regional responses and monitor the next deployment or traffic peak.
Common 502 Bad Gateway Mistakes
Common mistakes hide the gateway failure, restart services without evidence, increase limits blindly or diagnose only one side of the proxy-to-upstream connection.
| Mistake | Why it fails | Preferred correction |
|---|---|---|
| Redirecting to the homepage | The upstream failure remains hidden and unresolved. | Return the accurate status and repair the exchange. |
| Returning 200 for the error page | The response falsely reports successful content delivery. | Keep the correct 5xx status. |
| Assuming the CDN is broken | The origin may be returning an invalid response. | Inspect edge and origin evidence together. |
| Restarting every service | Evidence is destroyed without identifying the cause. | Capture logs and state before controlled recovery. |
| Increasing all timeouts | A 502 may not be a timeout problem. | Use the exact gateway error message. |
| Disabling TLS validation | The upstream connection becomes vulnerable. | Correct certificate and trust configuration. |
| Testing only from one region | Edge or routing failures may be regional. | Compare multiple authorized locations. |
| Checking only the homepage | The problem may affect one route or upstream service. | Test representative URL patterns. |
| Ignoring intermittent failures | One healthy backend can mask another failing instance. | Review target-level error distribution. |
| Closing after one successful request | The failure can recur under traffic or deployment conditions. | Monitor through a defined observation window. |
502 Bad Gateway Checklist
A 502 task passes QA when the responding gateway, failed upstream exchange, verified root cause and stable recovery are all documented with evidence.
- The exact failed URL is recorded.
- The request method is recorded.
- The failure timestamp is recorded.
- The public 502 response is captured.
- Response headers are preserved.
- The responding gateway is identified.
- The gateway request ID is recorded.
- The selected upstream is identified.
- The upstream host is verified.
- The upstream port is verified.
- The upstream protocol is verified.
- Gateway logs are reviewed.
- Upstream logs are reviewed.
- Service health is checked.
- Container health is checked where relevant.
- Socket availability is checked.
- Socket ownership is checked.
- DNS resolution is checked from the gateway.
- TLS certificate validity is checked.
- SNI and Host headers are checked.
- Firewall rules are reviewed.
- Recent deployments are compared.
- Regional differences are tested.
- Target-level error rates are reviewed.
- The root cause is documented.
- The correction is deployed safely.
- Representative URLs are retested.
- Important URLs are recrawled.
- Monitoring confirms no recurrence.
- A prevention action is assigned.
Frequently Asked Questions About 502 Errors
What does 502 Bad Gateway mean?
Is a 502 error a browser problem?
What causes a 502 Bad Gateway error?
What is the difference between 502 and 500?
What is the difference between 502 and 503?
What is the difference between 502 and 504?
Can a CDN return a 502 error?
Can Nginx return 502?
Can DNS cause 502?
Can SSL cause a 502 error?
Are 502 errors bad for SEO?
How can I check whether a URL returns 502?
Trace the Gateway Failure From Edge to Origin
Start with the exact URL and capture the public status, response headers, timestamp and gateway request ID. Then identify the selected upstream server and compare CDN, proxy, load-balancer and application evidence from the same request window.
Use the free HTTP checker for one URL, measure the public serving path with the Server Response Time Test and use Crawl Explorer or Site Health Audit when the error affects a larger URL set.
Novaverb connects live status codes, redirect paths, crawl evidence and server timing so teams can distinguish one broken URL from a systemic edge-to-origin failure.
Use the Decision Ladder to determine whether the next action should be upstream recovery, proxy correction, TLS repair, deployment rollback or infrastructure escalation.