Skip to main content
Fixing 502 and 504 Errors from Kubernetes Ingress

Fixing 502 and 504 Errors from Kubernetes Ingress

KU
· Founder & Chief Editor
• 8 min read•Updated •55 views

A 502 from your ingress means no healthy backend to talk to. A 504 means the backend answered too slowly. Here's how to tell them apart and fix each.

Key takeaways

  • An ingress 502 means the proxy could not get a usable response from any upstream: empty Endpoints, a wrong targetPort, an app bound to localhost, or a TLS mismatch.
  • A 504 means the backend answered slower than proxy-read-timeout, 60 seconds by default in ingress-nginx.
  • Read the controller log line before changing anything.

Read the ingress controller log first. A 502 with connect() failed or Connection refused means no healthy upstream: check kubectl get endpoints, readiness probes and targetPort. A 504 with upstream timed out means the backend was slower than proxy-read-timeout (60 seconds by default): fix backend latency before raising timeouts.

Verified against

ingress-nginx v1.15.1 (final release; repository archived March 24, 2026), against the ingress-nginx annotations and ConfigMap docs and the kubernetes.io container lifecycle hooks page. Checked October 2026.

When the pager goes off with a screenshot of a 502 page, the first instinct is to blame the app. Usually the app is fine and the ingress just has nobody healthy to talk to. The two codes point at different problems, and the fixes don't overlap.

What the two codes actually mean at the ingress

The ingress controller (ingress-nginx, in most of the clusters we've touched) is a reverse proxy. It takes the request, picks an upstream pod, opens a connection, and waits for a response. Two things can go wrong there.

A 502 Bad Gateway means the proxy tried to reach an upstream and the connection failed. No healthy endpoint to route to, connection refused, connection reset mid-flight, or the backend spoke a protocol the proxy didn't expect. The proxy never got a usable response, so it gives up and returns 502.

A 504 Gateway Timeout means the proxy did connect, sent the request, and then sat there waiting until its own read timeout expired. The backend is alive but too slow. That's a different animal entirely.

Our rule of thumb: 502 is a connectivity problem, 504 is a latency problem. Don't touch timeouts for a 502, and don't restart pods for a 504.

Diagnosis before you touch anything

Start at the ingress controller logs. That's the source of truth for what the proxy saw.

code
$ kubectl logs -n ingress-nginx deploy/ingress-nginx-controller | grep -E 'connect\(\) failed|upstream timed out'

A real 502 line looks like this:

code
2026/07/12 09:14:03 [error] 41#41: *88213 connect() failed (111: Connection refused)
while connecting to upstream, client: 10.0.3.11, server: api.example.com,
upstream: "10.244.2.7:8080", request: "GET /v1/orders HTTP/1.1"

Note the upstream: "10.244.2.7:8080". That's a specific pod IP and port the proxy tried. Now check whether that endpoint should even exist:

code
$ kubectl get endpointslices -l kubernetes.io/service-name=my-api
$ kubectl get endpoints my-api -o wide

If the EndpointSlice lists no ready addresses, or the legacy endpoints view prints <none> under ENDPOINTS, you've found your 502 already. The Service has no ready pods behind it, so the ingress has nowhere to send traffic. From there it's a readiness question:

code
$ kubectl get pods -l app=my-api
$ kubectl describe pod my-api-xxxx | grep -A5 Readiness

For a 504, the log reads differently:

code
2026/07/12 09:20:41 [error] 41#41: *91002 upstream timed out
(110: Connection timed out) while reading response header from upstream,
upstream: "10.244.2.7:8080", request: "GET /v1/report HTTP/1.1"

upstream timed out ... while reading response header is the signature. The connection worked; the wait didn't.

Fixing the 502

Once you know it's a connectivity failure, walk the causes roughly in order of how often they bite.

No ready endpoints. The most common one. A deploy rolled out, the new pods failed their readiness probe, and the old ones already terminated. The Endpoints list is empty. Fix the probe or fix whatever the probe is correctly catching. Don't paper over it by deleting the readinessProbe; that just moves the 502 to your users instead of your dashboard.

Wrong service or targetPort. The Service targetPort doesn't match the port the container actually listens on. Everything looks green because pods are Ready, but the proxy connects to a port nothing's bound to and gets a refusal. Check both sides:

code
$ kubectl get svc my-api -o jsonpath='{.spec.ports[0].targetPort}'

Then compare against the container's real listen port. This one hides well because the pods are healthy.

App not listening on the interface. Classic: the app binds to 127.0.0.1 instead of 0.0.0.0. Inside the pod it works, from another pod it refuses. Connection refused in the logs, every time.

Backend crashing under load. Connection reset (104) instead of refused (111) usually means the pod accepted the connection then died or got OOM-killed mid-request. Check kubectl get pods for restart counts and look for OOMKilled in the pod events.

TLS/protocol mismatch. If your backend serves HTTPS but the ingress is talking plain HTTP to it, you get a 502. Nginx needs to be told the backend is secure:

YAML
# ingress.yaml (metadata.annotations)
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"

Miss that annotation and the proxy sends cleartext into a TLS listener, which resets the connection.

Pods terminating without draining. During a rollout, a pod gets SIGTERM and is pulled from Endpoints, but in-flight connections still land on it for a second or two because of propagation lag. Add a preStop hook with a short sleep so the pod keeps serving while it's being removed from the endpoint list. Kubernetes now has a built-in sleep handler, so you don't need a shell in the image:

YAML
# deployment.yaml (spec.template.spec)
terminationGracePeriodSeconds: 45
containers:
  - name: my-api
    lifecycle:
      preStop:
        sleep:
          seconds: 15

The grace period has to cover the sleep plus your app's own shutdown, because the countdown starts before the hook runs. A fifteen-second sleep has killed more rollout 502s for us than any other single change. Our post on readiness probes that lie during rolling updates covers the rest of that race.

Fixing the 504

Every 504 comes down to the same thing: the backend took longer than the proxy was willing to wait. You either speed up the backend or extend the wait, and usually you need both.

The ingress-nginx ConfigMap defaults are proxy-read-timeout: 60, proxy-send-timeout: 60 and proxy-connect-timeout: 5, all in seconds. If your report endpoint sometimes takes 90, it gets cut off at 60 and the user sees a 504 even though the backend would have answered. Bump the timeouts on that ingress:

YAML
# ingress.yaml (metadata.annotations)
nginx.ingress.kubernetes.io/proxy-read-timeout: "120"
nginx.ingress.kubernetes.io/proxy-send-timeout: "120"
nginx.ingress.kubernetes.io/proxy-connect-timeout: "10"

Keep proxy-connect-timeout short. Connecting should be instant on a healthy network; a long connect timeout just delays the 502 you were going to get anyway.

If the slowness is a fat response streaming through, buffering settings matter too. Buffering is off by default and proxy-buffer-size defaults to 4k:

YAML
# ingress.yaml (metadata.annotations)
nginx.ingress.kubernetes.io/proxy-buffering: "on"
nginx.ingress.kubernetes.io/proxy-buffer-size: "16k"

But be honest about root cause. Most 504s we've chased were a slow database query behind the endpoint, not the proxy. Raising the timeout to 120 seconds turns a fast failure into a slow one and hides the real problem: a missing index or a lock. Pull the backend's own latency metrics before you reach for the timeout annotation. If p99 is climbing, the ingress is the messenger, not the culprit.

A note on ingress-nginx in 2026

The community ingress-nginx project is retired. Best-effort maintenance ended in March 2026, the repository was archived on March 24, 2026, and there are no further releases or security fixes; v1.15.1 is the last. The diagnosis above still applies to clusters running it, and the 502/504 split is the same for any proxy, but plan the move. Our Ingress to Gateway API migration guide covers how.

For deeper pod-level and networking failures that these codes surface, our Kubernetes troubleshooting guide covers the layers underneath.

Questions people ask

What is the difference between a 502 and a 504 from Kubernetes ingress?

A 502 Bad Gateway means the ingress proxy never got a usable response: no ready endpoints, connection refused, connection reset, or a protocol mismatch such as HTTP sent to an HTTPS backend. A 504 Gateway Timeout means it connected and sent the request, but the backend didn't answer within proxy-read-timeout. Treat 502 as connectivity and 504 as latency.

How do I increase the ingress-nginx timeout?

Add nginx.ingress.kubernetes.io/proxy-read-timeout and proxy-send-timeout annotations to the Ingress, with values in seconds as quoted strings, for example "120". The defaults come from the controller ConfigMap: 60 seconds for read and send, 5 for connect. Change the ConfigMap only if every Ingress needs it. Check backend latency first, because a longer timeout often hides a slow query.

Why do I get 502 errors only during deployments?

Terminating pods keep receiving traffic for a moment after they leave the endpoint list, because removal propagates to the controller asynchronously. Add a preStop sleep of 10 to 15 seconds, make sure terminationGracePeriodSeconds covers it plus shutdown, and confirm new pods only pass readiness when they can actually serve. A failing readiness probe during rollout can also empty the endpoint list entirely.

The call we'd make

Read the ingress log line first, always. If it says connect() failed or connection reset, it's a 502 and you go check Endpoints and readiness, not timeouts. If it says upstream timed out, it's a 504 and you look at backend latency before you touch a single annotation. The worst outages we've watched came from someone bumping proxy-read-timeout on a 502, or bouncing pods on a 504, because they never read the log line. Two minutes with kubectl logs and kubectl get endpoints beats an hour of guessing.

Sources

Explore topics:KubernetesDevOps
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). Fixing 502 and 504 Errors from Kubernetes Ingress. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/fix-ingress-502-504

Markdown

[Fixing 502 and 504 Errors from Kubernetes Ingress](https://www.devopsness.com/blog/fix-ingress-502-504) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.