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 This Error 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
Code Examples
Here's how to return a 502 status code in various programming languages:
Python (Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/endpoint')
def endpoint():
return jsonify({"message": "Bad Gateway"}), 502
Node.js (Express)
const express = require('express');
const app = express();
app.get('/endpoint', (req, res) => {
res.status(502).json({ message: 'Bad Gateway' });
});
PHP
<?php
http_response_code(502);
header('Content-Type: application/json');
echo json_encode(['message' => 'Bad Gateway']);
?>
Go
package main
import (
"encoding/json"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(502)
json.NewEncoder(w).Encode(map[string]string{
"message": "Bad Gateway",
})
}
Java (Spring Boot)
@GetMapping("/endpoint")
public ResponseEntity<Map<String, String>> endpoint() {
Map<String, String> response = new HashMap<>();
response.put("message", "Bad Gateway");
return ResponseEntity.status(502).body(response);
}
Ruby (Sinatra)
get '/endpoint' do
status 502
json message: 'Bad Gateway'
end
Browser Compatibility
| Browser | Support | Notes |
|---|---|---|
| Chrome | ✓ Full Support | All versions |
| Firefox | ✓ Full Support | All versions |
| Safari | ✓ Full Support | All versions |
| Edge | ✓ Full Support | All versions |
Official Specification
This status code is defined in RFC 7231 Section 6.6.3.