504 Gateway Timeout
Fault: Server
TL;DR
Gateway/proxy timeout waiting for upstream server.
The server did not receive a timely response from the upstream server.
What This Error Means
The server did not receive a timely response from the upstream server.
Gateway/proxy timeout waiting for upstream server.
Common Causes
- Upstream server too slow
- Database query timeout
- Slow application response
- Network latency
- Long-running script
How to Fix It (For Visitors)
- Refresh the page
- Try again later
- Contact website administrator
How to Fix It (For Developers/Admins)
- Increase gateway timeout (proxy_read_timeout)
- Optimize slow database queries
- Add indexes to database
- Implement caching
- Optimize application code
- Use async processing for long tasks
Code Examples
Here's how to return a 504 status code in various programming languages:
Python (Flask)
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/endpoint')
def endpoint():
return jsonify({"message": "Gateway Timeout"}), 504
Node.js (Express)
const express = require('express');
const app = express();
app.get('/endpoint', (req, res) => {
res.status(504).json({ message: 'Gateway Timeout' });
});
PHP
<?php
http_response_code(504);
header('Content-Type: application/json');
echo json_encode(['message' => 'Gateway Timeout']);
?>
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(504)
json.NewEncoder(w).Encode(map[string]string{
"message": "Gateway Timeout",
})
}
Java (Spring Boot)
@GetMapping("/endpoint")
public ResponseEntity<Map<String, String>> endpoint() {
Map<String, String> response = new HashMap<>();
response.put("message", "Gateway Timeout");
return ResponseEntity.status(504).body(response);
}
Ruby (Sinatra)
get '/endpoint' do
status 504
json message: 'Gateway Timeout'
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.5.