502 Bad Gateway
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
- Upstream server down
- PHP-FPM not running
- Application server crashed
- Network issue between servers
- Firewall blocking connection
How to Fix It (For Visitors)
- Refresh the page
- Try again in a few minutes
- Contact website administrator
How to Fix It (For Developers/Admins)
- Check upstream server status
- Restart PHP-FPM/application server
- Check Nginx proxy_pass configuration
- Verify firewall rules
- Check application server logs
- Increase timeout settings
Read the nginx Error Log Message
When nginx is the proxy, its error log names the exact problem:
| Log message | Meaning |
|---|---|
connect() failed (111: Connection refused) while connecting to upstream | Nothing 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 connection | The app crashed or was killed mid-request (often memory limits). |
upstream sent too big header | Response headers exceed the buffer - raise proxy_buffer_size / fastcgi_buffer_size. |
no live upstreams | All 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.