# 504 Gateway Time-out in nginx

> Fix an nginx 504 Gateway Time-out: read the upstream timed out log line, then tune proxy_read_timeout, fastcgi_read_timeout and the PHP-FPM limits behind it.

Canonical: https://scaplet.com/errors/http/504/nginx

Guide Error · HTTP status codes

Fix an nginx 504 Gateway Time-out: read the upstream timed out log line, then tune proxy_read_timeout, fastcgi_read_timeout and the PHP-FPM limits behind it.

3 min

read

8

sources

Oct 22, 2026

updated

Scaplet Editorial Team

author

504 5xx · Server error

The short answer

In nginx, a 504 Gateway Time-out means nginx waited for the upstream (an app, PHP-FPM or another server) longer than a timeout allows and gave up. The error log says `upstream timed out` and names the phase. Find which timeout fired, then fix the slow upstream first and raise `proxy_read_timeout` or `fastcgi_read_timeout` only if the work is meant to be slow.

1. [Why nginx returns a 504](#why-nginx-returns-a-504)
2. [Fix it: slow upstream first, then the timeout](#fix-it-slow-upstream-first-then-the-timeout)
3. [Verify the fix](#verify-the-fix)

30-second check

## Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?

Summarize this page with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F504%2Fnginx%20(%22504%20Gateway%20Time-out%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F504%2Fnginx%20(%22504%20Gateway%20Time-out%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F504%2Fnginx%20(%22504%20Gateway%20Time-out%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F504%2Fnginx%20(%22504%20Gateway%20Time-out%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Which timeout fired

## Why nginx returns a 504

RFC 9110 defines 504 as the status for a server that, "while acting as a gateway or proxy, did not receive a timely response from an upstream server". nginx is that gateway when you use `proxy_pass` or `fastcgi_pass`. The general meaning is on [504 Gateway Timeout](/errors/http/504); a refused or invalid reply is a 502 instead, covered on [502 Bad Gateway in nginx](/errors/http/502/nginx). This page covers the nginx timeouts.

Log phase (after `upstream timed out`)

### `while connecting to upstream`

Directive

`proxy_connect_timeout` / `fastcgi_connect_timeout`

Default

`60s`; the docs note this timeout cannot usually exceed 75 seconds

Log phase (after `upstream timed out`)

### `while sending request to upstream`

Directive

`proxy_send_timeout` / `fastcgi_send_timeout`

Default

`60s`, measured between two successive writes

Log phase (after `upstream timed out`)

### `while reading response header from upstream`

Directive

`proxy_read_timeout` / `fastcgi_read_timeout`

Default

`60s`, measured between two successive reads, not for the whole response

The nginx source answers with a 504 when an upstream connect, send or read timer expires, and logs `upstream timed out` followed by the phase it was in. That phase tells you which directive to look at.

Find it in the logs

```
# The phase is at the end of the line sudo grep "upstream timed out" /var/log/nginx/error.log | tail -n 5 # Then time the upstream directly curl -sS -o /dev/null -w "connect %{time_connect}s total %{time_total}s status %{http_code}\n" http://127.0.0.1:8000/slow-page
```

_02_Fix it on your stack

## Fix it: slow upstream first, then the timeout

A longer timeout hides a slow upstream; it does not repair it. The read timeouts count the gap between two reads, so a response that keeps sending data can run longer than 60 seconds without a 504, and one that stays silent for 60 seconds cannot.

Raise the read timeout for one slow endpoint only
Raise the read timeout for one slow endpoint only

```
location /reports/ { proxy_pass http://127.0.0.1:8000; proxy_connect_timeout 5s; # fail fast on a dead upstream proxy_read_timeout 300s; # this endpoint is meant to be slow }
```

The same for PHP-FPM
The same for PHP-FPM

```
location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/run/php/php-fpm.sock; fastcgi_read_timeout 300s; }
```

Log timings to see where the time goes
Log timings to see where the time goes

```
log_format timing '$remote_addr "$request" $status ' 'rt=$request_time connect=$upstream_connect_time urt=$upstream_response_time'; access_log /var/log/nginx/timing.log timing;
```

Causes and fixes
| Cause | Where to look | Fix |
| The app takes longer than the read timeout | `$upstream_response_time` in an access log format, and the app's own slow log | Speed up the request, or move the work to a background job. Raise the read timeout only for the one location that needs it |
| PHP stops the script first | `request_terminate_timeout` (default `0`, off) in the pool, and PHP's `max_execution_time` (default 30) | PHP's `max_execution_time` stops a script after 30 seconds by default, and `request_terminate_timeout` kills the worker when set. If a script is meant to run longer, raise the PHP limit and `fastcgi_read_timeout` together |
| Upstream unreachable at connect time | `$upstream_connect_time` and firewall rules | Fix the route or the service. The connect timeout is not a place to wait out a dead host |
| Retries make a failure look longer | `proxy_next_upstream` (default `error timeout`) | With a group of servers, nginx can try the next one after a timeout, so the visible delay can be several timeouts long |
| Another timer in front of nginx | A CDN or load balancer's own limit | Compare the timeout at each hop; the smaller one fires first |

_03_How it works

## Verify the fix

A 504 is a 5xx response, and repeated 5xx responses can slow Google's crawling; see [5xx server errors](/seo-issues/server-errors-5xx). If visitors give up before nginx does, nginx logs 499, covered on [499 Client Closed Request](/errors/http/499).

1. 01

### Reload cleanly

Run `sudo nginx -t && sudo nginx -s reload`.
2. 02

### Repeat the slow request

Request the page that failed and confirm a 2xx. Compare `$request_time` with the timeout you set; if it is close, the next slow request will fail again.
3. 03

### Check the log

The `upstream timed out` line should stop appearing for that URL.

Questions

## Asked a lot.

5 questions people ask next, answered from the sources on this page.

_01_ How do I fix 504 Gateway Timeout in nginx?

Read the error log for `upstream timed out` and the phase after it. Then speed up the upstream, or raise `proxy_read_timeout` (or `fastcgi_read_timeout` for PHP-FPM) on the one location that needs it, and reload nginx.

_02_ What does error code 504 mean in nginx?

nginx, acting as a gateway, did not get a timely response from the upstream server. RFC 9110 defines the status, and nginx's connect, send and read timeouts decide when it gives up.

_03_ Is a 504 error my fault?

As a visitor, no: the delay is between the site's servers. As the site owner, yes, it is yours to fix, in the upstream's speed or the timeouts between nginx and the upstream.

_04_ What is the default nginx proxy_read_timeout?

60 seconds. It applies between two successive read operations, not to the whole response.

_05_ Why do I get a 504 even after raising proxy_read_timeout?

A different timer can fire first: the connect timeout, PHP's own limits, or a CDN or load balancer in front of nginx. The phase in the `upstream timed out` log line shows which.

Sources · 8

## Every fact, linked and dated.

Official docs first. The date is when we last opened each page.

- rfc-editor.org
- nginx.org
- github.com
- php.net
- google.com

1. 01 [RFC 9110: HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110.html) rfc-editor.org · checked 2026-10-25
2. 02 [nginx: ngx_http_proxy_module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html) nginx.org · checked 2026-10-25
3. 03 [nginx: ngx_http_fastcgi_module](https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html) nginx.org · checked 2026-10-25
4. 04 [nginx: ngx_http_upstream_module](https://nginx.org/en/docs/http/ngx_http_upstream_module.html) nginx.org · checked 2026-10-25
5. 05 [nginx source: src/http/ngx_http_upstream.c](https://github.com/nginx/nginx/blob/master/src/http/ngx_http_upstream.c) github.com · checked 2026-10-25
6. 06 [PHP manual: FPM configuration](https://www.php.net/manual/en/install.fpm.configuration.php) php.net · checked 2026-10-25
7. 07 [PHP manual: Runtime configuration (max_execution_time)](https://www.php.net/manual/en/info.configuration.php) php.net · checked 2026-10-25
8. 08 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-10-25

## Keep learning

[All errors](/errors)

[Learn 504 Gateway Timeout HTTP status codes](/errors/http/504) [Related 5xx server errors Status codes and links](/seo-issues/server-errors-5xx) [Read next 502 Bad Gateway HTTP status codes](/errors/http/502) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 504 errors
[ALB 504 Gateway Timeout](/errors/cdn/alb-504-gateway-timeout) [API Gateway 504 Gateway Timeout](/errors/cdn/api-gateway-504) [Cloudflare 504 Gateway Timeout](/errors/cdn/cloudflare-504) [504 Gateway Timeout](/errors/http/504) [504 Gateway Timeout in Apache](/errors/http/504/apache)

Nearby in errors
[505 HTTP Version Not Supported](/errors/http/505) [506 Variant Also Negotiates](/errors/http/506)

More in 504 Gateway Timeout
[504 Gateway Time-out in nginx](/errors/http/504/nginx) [504 Gateway Timeout in Apache](/errors/http/504/apache) [504 Gateway Timeout on IIS](/errors/http/504/iis)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

Run the free audit, then approve the fixes. Every change can be undone.

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
