Skip to content Use Monity from Claude, ChatGPT or Codex: set up monitors and ask what changed, from a chat. Use Monity from Claude, ChatGPT or Codex Connect yours
Tutorial 4 min read

HTTP 502 Bad Gateway: what the proxy’s error log tells you

A 502 Bad Gateway means a proxy, load balancer or CDN got a broken answer from the app behind it. Which layer answered, the nginx error log lines that name the cause, 502s that come and go with deploys and load, and what visitors can do.

Alex Lead Product Designer
UpdatedNovember 23, 2026 Read time4 min AudienceSite owners and developers

A 502 Bad Gateway means a server in the middle, such as nginx, a load balancer or a CDN, asked the app behind it for the page and got a broken answer, or none at all. The app crashed, refused the connection or hung up halfway. It’s a fault between the site’s own servers, so visitors can only wait. If it’s your site, the proxy’s error log names the cause in one line.

Where does a 502 come from?

A request to a modern site passes through several servers before it reaches the code that builds the page:

  1. your browser,
  2. a CDN such as Cloudflare,
  3. a load balancer,
  4. a web server such as nginx, acting as a reverse proxy,
  5. the app: PHP-FPM, a Node, Python or Ruby process, or a container.

Any server from step 2 to step 4 can be the “gateway”. RFC 9110 defines 502 as a gateway or proxy that “received an invalid response from an inbound server”. So the server that sent the 502 is working; the one behind it isn’t. If the app had answered with a proper error, the proxy should pass that on, and if it hadn’t answered in time, the code would be 504 (MDN).

Which layer answered?

  • A plain page with “nginx” under the title is nginx’s own: look at nginx’s error log and the app behind it.
  • A Cloudflare-branded 502 passes on a 502 from your server. A blank, unbranded 502 comes from Cloudflare itself, for example when the origin sends broken gzip or a wrong Content-Length. Through Cloudflare Tunnel, it means cloudflared can’t reach the service in its ingress rule (Cloudflare).
  • Behind an AWS Application Load Balancer, the load balancer answers 502 when a target resets or closes the connection, or sends headers it can’t accept (AWS).

The error log names the cause

On most Linux installs nginx logs to /var/log/nginx/error.log. Read it at the time of the 502:

sudo tail -n 50 /var/log/nginx/error.log
Log line contains What happened Fix
connect() failed … while connecting to upstream Nothing accepted the connection: the app is down, restarting, or listening somewhere else Start the app; compare the address in proxy_pass or fastcgi_pass with where it listens
upstream prematurely closed connection The app hung up mid-request: it crashed, was killed for memory, or closed a kept-alive connection Read the app’s own log and the system log for kills
upstream sent too big header The response headers didn’t fit proxy_buffer_size, one memory page (4 or 8 KB) by default; big cookies are the usual reason Raise proxy_buffer_size (fastcgi_buffer_size for PHP), and trim the cookies

An AWS load balancer has its own limit: 32 KB for a response’s headers. Past it, the answer is a 502.

502s that come and go

  • During deploys. If every app process restarts at once, nothing answers for a few seconds. Graceful reloads or rolling restarts keep old processes serving until the new ones are ready.
  • Behind a load balancer. AWS names a common one: the app’s keep-alive timeout is shorter than the load balancer’s idle timeout (60 seconds by default), so the app closes connections the load balancer is about to reuse. Set the app’s keep-alive longer than the idle timeout.
  • Under load. A process that runs out of memory is killed, and the requests it held get a 502. The system log shows the kill.
The failed check of a Monity uptime incident: HTTP status, response time, server address, the URL checked, the certificate, and the time spent on DNS lookup, connect, TLS handshake and first byte
The failed check behind a Monity incident (this one a 503): the status, the server that answered and where the time went. A refused connection makes a 502 come back quickly; a 504 only comes after the proxy’s timeout.

Intermittent 502s are the hardest to catch by hand. Monity’s uptime monitoring requests the page from outside on a schedule, checks a failure twice more within seconds, and keeps the failed answer, so a 502 at 3 a.m. still has a time, a status and headers when you look in the morning.

Seeing a 502 on someone else’s site?

Reload after a minute; a 502 during a deploy often clears that fast. If it lasts, check the company’s status page. It’s only on your side if the site works for everyone else and you use a VPN or a proxy: try another network.

More: 504 Gateway Timeout, 503 Service Unavailable and all HTTP status codes.

FAQ

Questions people ask

What’s the difference between 502 and 504?

Both come from a proxy or load balancer. A 502 means the server behind it gave a broken answer or refused the connection. A 504 means it gave no answer in time. A 502 points at something down or crashed, a 504 at something slow.

Is a 502 Bad Gateway my fault?

Almost never. It happens between the site’s own servers. The exception is a VPN or proxy on your side: if the site works for everyone else, turn it off or try another network.

How do I fix a 502 with nginx and PHP-FPM?

Check that PHP-FPM is running, then compare the socket or port in nginx’s fastcgi_pass with the one PHP-FPM listens on: an upgrade can change the socket’s path. “upstream sent too big header” in the log means raising fastcgi_buffer_size.

Why do I get a 502 only during deploys?

Because for a moment no app process accepts connections. Restart processes in turn, or use a graceful reload, so something always answers.

Does a 502 error hurt SEO?

Short ones, hardly. Google slows its crawling when it meets server errors and keeps indexed pages for a while, but URLs that keep returning them are eventually dropped.

Written by

Alex

Alex leads product design at Monity. From the plain-English setup to the way an alert is laid out, her work is about making website monitoring something anyone can use, technical or not. She writes here about product design and usability.

More by author
Kamil, Alex and Martyna A person replies within two working days

Have a question?

Pricing, security or scoping: pick one and your message goes to a person on the team.