DB / Nginx / 504
WARNING
Nginx Gateway Time-out
The Nginx server, acting as a reverse proxy or gateway, did not receive a timely response from an upstream server (e.g., PHP-FPM, Node.js, another backend service) within its configured timeout period.
Common Causes
- The upstream application server (e.g., PHP, Python, Java) is overloaded, crashed, or processing a request too slowly.
- Network connectivity issues or high latency between Nginx and the upstream server.
- Insufficient Nginx proxy timeout settings (`proxy_read_timeout`, `proxy_connect_timeout`) for long-running backend operations.
- The upstream server is waiting on a resource (like a database or external API) that is itself timing out.
How to Fix
1 Increase Nginx Proxy Timeouts
Adjust the timeout directives in your Nginx server or location block to give the upstream server more time to respond.
BASH
$ location / {
proxy_pass http://backend;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 120s; # Increase this value
} 2 Check Upstream Server Health & Logs
Investigate the application server (e.g., PHP-FPM, Gunicorn) for errors, high load, or slow queries. Check its logs.
BASH
$ # Check application server status (example for PHP-FPM)
systemctl status php8.1-fpm
# Tail application error logs
tail -f /var/log/php8.1-fpm.log
# Check for high server load
uptime
top 3 Optimize Backend Application
Identify and fix slow database queries, inefficient code, or external API calls in your application that cause the delay.
BASH
$ # Enable slow query log in MySQL to find problematic queries
# Add to /etc/mysql/my.cnf under [mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2