A 504 error means that Nginx, acting as a gateway or proxy, never got a response from the upstream server: PHP-FPM, a Node.js or Python application, or another web server. The request reached the backend, but it didn't respond in time, so Nginx returned a 504 to the client.

The key thing to remember right away: increasing the timeout only treats the symptom. In most cases the real cause is a slow backend, heavy database queries, or insufficient resources. Below we'll walk through a diagnostic order (logs, backend, resources, timeouts) and the specific directives you need to check.

This article is aimed at site owners and VPS administrators running Nginx as a reverse proxy or gateway to a FastCGI application. If you're seeing a 504 on someone else's site, there's almost nothing you can do from your end except try reloading the page later.

What 504 means and how it differs from 502

Nginx often sits in front of an application and forwards requests to it. If you're not familiar with the role of a web server, start with the article “What Is a Web Server and What Is It For”; here we'll focus specifically on the error.

Nginx has two types of timeouts tied to the upstream: one for establishing the connection, and one for waiting on a response. If either one expires with no response, the client gets a 504. In the browser, the error shows up as a blank page with the heading “504 Gateway Time-out” and a signature line reading nginx (with or without the version number, depending on the server_tokens setting).

The error.log will usually show a line containing the words upstream timed out (110: Connection timed out). This is the most reliable signal, and it's what distinguishes a 504 from other gateway errors.

504 is often confused with 502. The difference lies in what exactly happened to the backend:

502 Bad Gateway

504 Gateway Time-out

What happened

Nginx got an invalid response or couldn't connect

No response arrived within the set time

Typical cause

Backend crashed, wrong socket or port, process restarting

Backend is alive but responding too slowly

What to look for in the log

connect() failed, Connection refused, upstream prematurely closed connection

upstream timed out (110: Connection timed out)

Where to start

Check whether the backend is running and the socket is reachable

Figure out why the backend is slow

The line between them isn't absolute. For example, if PHP-FPM forcibly kills a long-running process, Nginx usually sees a dropped connection and returns a 502 instead of a 504. So rely on the log text, not just the status code.

Main causes of 504

There are several possible causes, and they often overlap. Start with the most likely ones.

Cause

How it shows up

Where to look

Slow or hung backend

504 on specific pages or operations

Application log, PHP-FPM slowlog, database slow query log

PHP-FPM pool exhausted

Requests queue up, error appears as traffic grows

PHP-FPM log, messages about pm.max_children

Not enough CPU, RAM, or swap

The whole server slows down, load increases

top, free -h, load average

Timeouts set too short

Long but legitimate operations (export, import) get cut off

Nginx config, *_timeout directives

Network or DNS issues between Nginx and upstream

Error when reaching an external or remote backend

curl from the server to upstream, DNS settings

Firewall blocking

Connection to upstream never gets established

Firewall rules, security groups

Step 1. Find the cause in the logs

Don't touch any configs until you've checked the logs. They'll tell you which upstream failed to respond and at what stage.

  1. Open the Nginx error log. On Debian and Ubuntu it's usually at /var/log/nginx/error.log, but a site may have its own file set via the error_log directive. Paths vary by distribution and installation method, so check your own config.

  2. Find the most recent timeout entries.

  3. Look at the address in the upstream field: it shows which backend Nginx was talking to (a PHP-FPM socket, an application port, an external address).

  4. Pay attention to the phrase after while: while reading response header from upstream means the connection was established but no response came; while connecting to upstream points to a connection problem.

  5. Check the PHP-FPM and application logs for the same time window.

sudo tail -n 50 /var/log/nginx/error.log
sudo grep "upstream timed out" /var/log/nginx/error.log | tail -n 20

A log line looks roughly like this (your addresses and paths will differ):

upstream timed out (110: Connection timed out) while reading response header from upstream,
client: 203.0.113.10, server: example.com, request: "GET /report/ HTTP/1.1",
upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:", host: "example.com"

If the cause isn't obvious, enable slowlog in PHP-FPM (the request_slowlog_timeout and slowlog directives in the pool configuration). It will log the call stack for requests that run longer than a set threshold, showing you which function got stuck.

Step 2. Check whether the backend is alive and resources are sufficient

Make sure the services are running and the server isn't hitting a resource ceiling.

  1. Check the status of the web server and backend via systemd.

  2. Check CPU load and memory usage.

  3. Check disk space and disk I/O load: a full disk or slow I/O can also cause timeouts.

  4. Look for signs of memory shortage in the kernel log: if the system was killing processes via OOM, the backend may have restarted mid-request.

  5. If the site runs on a database, enable the slow query log and check for queries that take tens of seconds.

sudo systemctl status nginx
sudo systemctl status php8.3-fpm   # the service name depends on your PHP version
uptime
free -h
df -h
sudo dmesg -T | grep -i "out of memory"

If load is high, figure out exactly what the metric is telling you. We explain how to read load average and when its values indicate a real problem in the article “Understanding Load Average (LA) in Linux and How It's Calculated”.

Result of command

If resources are consistently maxed out even under normal traffic, code optimization alone may not be enough, and it's worth reviewing your VPS configuration. But first make sure the cause really is a resource shortage and not just one heavy query.

Step 3. Configure Nginx timeouts

Timeouts should be adjusted only after diagnosis, and only where a long request duration is expected. The directives can be set in the http, server, and location contexts — the narrower the context, the more precise the tuning.

An important detail: read and write timeouts are measured between two consecutive operations, not over the entire request. All of the directives listed below default to 60 seconds, but check the Nginx documentation for your version before changing anything.

Example for PHP-FPM and for a proxied application (the values here are illustrative — tune them to your own needs):

# PHP via FastCGI
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_read_timeout 120s;
}
# Proxied application
location /api/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_connect_timeout 5s;
    proxy_send_timeout 60s;
    proxy_read_timeout 120s;
}

After making changes, check the syntax and reload the configuration without stopping the server:

sudo nginx -t
sudo systemctl reload nginx

reload Nginx

Important. Timeouts that are too long mask the underlying problem and tie up connections and worker processes. If a single page genuinely needs a multi-minute timeout, it's better to move that task into a background queue rather than stretching the wait time for the entire site.

Step 4. Configure PHP-FPM or the application

Raising the Nginx timeout alone won't help if the backend itself is interrupting or delaying execution.

What to check in PHP-FPM:

  • pm, pm.max_children, and related process management settings in the pool configuration (on Debian and Ubuntu, usually /etc/php/<version>/fpm/pool.d/www.conf). If the log shows a message that the pm.max_children limit has been reached, requests are waiting for a free process and hitting the timeout.

  • request_terminate_timeout: after this amount of time, FPM kills the process. It's disabled by default, but many hosting setups set it explicitly.

  • max_execution_time in php.ini: limits how long a script can run. If it's shorter than the Nginx timeout, the script will be cut off first, producing a different error.

These values should be tuned to the server's RAM. There's no universal number, but there is a method: estimate the average memory usage of a single PHP-FPM process (via ps or top, for example), figure out how much memory can be allocated to PHP after accounting for the database and the system, and divide one by the other. That gives you a reasonable upper bound for pm.max_children. Setting it too high leads to memory exhaustion and OOM kills — which is itself a new cause of 504 and 502 errors.

; example pool settings, tune the values to your server
pm = dynamic
pm.max_children = <calculate based on memory>
request_terminate_timeout = 120s
request_slowlog_timeout = 10s
slowlog = /var/log/php8.3-fpm.slow.log

After making changes, restart the PHP-FPM service and check whether the error is gone. You'll need to confirm the PHP version and paths on your own system — version 8.3 is used above purely as an example.

Other causes: network, firewall, CDN, and proxies in front of Nginx

Sometimes the 504 isn't even coming from the Nginx on your server. If a CDN, load balancer, or external proxy sits in front of your site, it might generate the error itself after failing to get a response from your server in time. Here's how to check:

  • Look at the response headers and the error page content: a CDN usually has its own template and its own headers.

  • Contact the server directly, bypassing the CDN — for example, with curl -I combined with --resolve to specify the IP address. If the server responds fine directly, the problem lies with the proxy or the connection between it and your server.

  • If Nginx is proxying requests to another server, check from that same server whether the upstream is reachable using curl, and verify name resolution. Nginx resolves the name in proxy_pass at startup, so after changing the upstream's IP you may need to reload the configuration.

  • Check the firewall: rules between servers can silently drop packets, in which case the connection never gets established and eventually times out.

How to prevent 504 errors

  1. Set up monitoring for CPU, memory, disk load, and site response time so you notice degradation before users see an error.

  2. Find slow queries via slowlog and the database log, add indexes, and rewrite heavy queries.

  3. Use page and object caching to reduce load on the backend.

  4. Move long-running operations (reports, imports, file processing) into background queues.

  5. Make sure the server has enough resource headroom for peak load.

  6. Track config changes and validate them with nginx -t before reloading.

Conclusion

A 504 error in Nginx means the backend didn't respond in time, and that's exactly where you need to look for the cause. Work through it in order: logs first, then backend state, then server resources, and only then timeouts. That way you'll fix the actual cause instead of just masking the symptom.

If your diagnosis shows that the load is consistently maxing out your resources, check Leneex's VPS plans page — your project may need more CPU or memory headroom. But keep in mind that upgrading your plan doesn't help in every case, so first rule out problems in the code and the database.

FAQ

What does 504 Gateway Time-out mean in Nginx?

It means Nginx forwarded the request to an upstream server (such as PHP-FPM or an application) but didn't get a response within the allotted time, so it returned a 504 to the client.

How is 504 different from 502?

With a 502, Nginx receives an invalid response or can't connect to the backend at all, usually because it's down. With a 504, the backend is reachable but takes too long to respond.

What should I set proxy_read_timeout to?

The default is 60 seconds. Only increase it for routes where a long response is genuinely expected, basing the value on the actual execution time of the longest legitimate request plus a small margin. For very long-running tasks, background processing is a better approach.

Why does 504 only appear on heavy pages?

Those are usually the pages running long database queries, calling external services, or generating large reports that don't fit within the timeout. Track down the cause using slowlog and application logs.

Can 504 be caused by insufficient VPS resources?

Yes. If CPU, memory, or disk performance is lacking, the backend responds more slowly and requests hit the timeout. But the root cause is often in the code or the database, so check the logs first.