HTTPError.net

The fastest way to diagnose, understand, and fix any HTTP status code

502 Bad Gateway

Fault: Server
TL;DR

Gateway/proxy received invalid response from upstream server.

The server received an invalid response from the upstream server.

What HTTP 502 Bad Gateway Means

The server received an invalid response from the upstream server.

Gateway/proxy received invalid response from upstream server.

Common Causes

How to Fix It (For Visitors)

How to Fix It (For Developers/Admins)

Read the nginx Error Log Message

When nginx is the proxy, its error log names the exact problem:

Log messageMeaning
connect() failed (111: Connection refused) while connecting to upstreamNothing is listening on the upstream address/port - the app or PHP-FPM is down or on another port.
connect() to unix:/run/php/php-fpm.sock failed (13: Permission denied)nginx cannot access the PHP-FPM socket - fix listen.owner/listen.group.
upstream prematurely closed connectionThe app crashed or was killed mid-request (often memory limits).
upstream sent too big headerResponse headers exceed the buffer - raise proxy_buffer_size / fastcgi_buffer_size.
no live upstreamsAll servers in the upstream group are marked as failed.

Step-by-step fixes are in the nginx 502 guide. Behind Cloudflare, origin problems usually show up as 520-526 errors instead.

Returning a 502 Bad Gateway (Code Examples)

If you build APIs or web apps, here is how to send an HTTP 502 response and how to test for it:

Node.js (Express)

app.get('/resource', (req, res) => {
  res.status(502).json({ error: 'Bad Gateway' });
});

Python (Flask)

@app.route('/resource')
def resource():
    return jsonify(error='Bad Gateway'), 502

PHP

<?php
http_response_code(502);
header('Content-Type: application/json');
echo json_encode(['error' => 'Bad Gateway']);

Check the status with curl

curl -I https://example.com/resource
# Look for: HTTP/1.1 502 Bad Gateway

Frequently Asked Questions

What does HTTP 502 Bad Gateway mean?

Gateway/proxy received invalid response from upstream server. In short: The server received an invalid response from the upstream server.

Is a 502 Bad Gateway error my fault or the website's?

502 is a 5xx server-error code, so the problem is on the server side, not your browser or device. As a visitor you can usually only retry; if you run the site, investigate the server.

How do I fix a 502 Bad Gateway error?

Check upstream server status. Restart PHP-FPM/application server. Check Nginx proxy_pass configuration.

Official Specification

The 502 Bad Gateway status code is defined in RFC 9110 Section 15.6.3.

View the IANA HTTP Status Code Registry →