Skip to content
  1. LuzineDebug
  2. DevOps, servers & cloud
  3. 2026/11/28 14:03:27 [error] 1234#1234: *89 upstr…
DevOps, servers & cloud Intermediate ≈ 20 min to fix

Fix "upstream timed out (110: Connection timed out)" and 504 Gateway Time-out in Nginx

Error message
2026/11/28 14:03:27 [error] 1234#1234: *89 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 203.0.113.7, server: example.com, request: "GET /api/report HTTP/1.1", upstream: "http://127.0.0.1:3000/api/report", host: "example.com"
  • 504 Gateway Time-out
  • upstream timed out (110: Connection timed out) while reading response header from upstream, upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"
  • upstream timed out (110: Connection timed out) while connecting to upstream
  • WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
  • HTTP/1.1 504 Gateway Timeout
In short

Nginx did reach your application (PHP-FPM, Node, Python…), but it did not answer within the allowed time (60 seconds by default), so Nginx returns 504. The real fix is to find and speed up the slow request; raising proxy_read_timeout or fastcgi_read_timeout is only a complement for legitimate long jobs.

Be the first to confirm this fix

What this error means

Nginx acts as a proxy: it forwards the request to the "upstream" server (your application) and waits for the header of its response. If nothing arrives before proxy_read_timeout (HTTP) or fastcgi_read_timeout (PHP-FPM), both 60 seconds by default, it gives up, writes upstream timed out (110: Connection timed out) to error.log and sends the browser a "504 Gateway Time-out" page. Unlike a 502, the application is reachable: it is just too slow, stuck, or so overloaded that the request waits in line. The while connecting to upstream variant means even the connection could not be established in time (firewall, wrong IP).

Common causes

  1. Genuinely slow request: SQL query without an index, slow external API call, export or PDF generation done during the HTTP request.
  2. Saturated application: every PHP-FPM worker is busy (server reached pm.max_children) or the Node event loop is blocked by synchronous work, and requests pile up.
  3. Inconsistent timeouts between layers: Nginx cuts at 60 s while PHP (max_execution_time) or the load balancer in front allows more, or the other way round.
  4. Database-side blocking: a lock, a transaction never committed or an exhausted connection pool; the application waits doing nothing.
  5. Unreachable upstream (while connecting to upstream): firewall, security group or wrong private IP between Nginx and the application.

Step-by-step fix

  1. Read the log and spot the slow URL

    The request: field gives the URL and upstream: the application involved. Then measure the response time directly on the application, bypassing Nginx.

    bash
    sudo tail -n 50 /var/log/nginx/error.log | grep "upstream timed out"
    curl -s -o /dev/null -w "status=%{http_code} total=%{time_total}s\n" http://127.0.0.1:3000/api/report
  2. Check the application is not saturated

    For PHP-FPM, look for the pm.max_children warning and enable the slow log, which prints the call stack of slow requests. For Node, check the process CPU: 100% on one core means a blocked event loop.

    ini
    sudo grep -i "max_children" /var/log/php8.3-fpm.log
    # /etc/php/8.3/fpm/pool.d/www.conf
    request_slowlog_timeout = 5s
    slowlog = /var/log/php-fpm-slow.log
  3. Fix the real cause

    Add the missing index, cache the result, or move the long job out of the HTTP request: a queue (Laravel Queues, BullMQ, Celery), then a notification or polling when it is ready. A web request should answer in a few seconds at most.

    sql
    EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 42 ORDER BY created_at DESC;
    CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at);
  4. Tune the workers if the server allows it

    If every worker is busy but memory is available, raise pm.max_children (roughly free memory / average worker memory), then restart PHP-FPM.

    ini
    ; /etc/php/8.3/fpm/pool.d/www.conf
    pm = dynamic
    pm.max_children = 20
    pm.start_servers = 4
    pm.min_spare_servers = 2
    pm.max_spare_servers = 6
  5. Raise the timeout for one route

    For a legitimate long job (report, import), raise the timeout only on that route, and align the other layers: PHP max_execution_time, PHP-FPM request_terminate_timeout, and the load balancer or Cloudflare timeout in front (100 s maximum on standard plans).

    nginx
    location /api/report {
        proxy_pass http://127.0.0.1:3000;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
    
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_read_timeout 300s;
    }
  6. Test and reload Nginx

    nginx -t validates the configuration before applying it; reload does not drop current connections.

    bash
    sudo nginx -t && sudo systemctl reload nginx
    sudo systemctl restart php8.3-fpm

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Monitor route response times (p95) and busy worker counts: a 504 rarely arrives without warning signs.
  • Run any job longer than a few seconds in a queue, never inside the HTTP request.
  • Document the timeout of each layer (CDN, load balancer, Nginx, PHP) and keep them consistent: the shortest one always wins.

Frequently asked questions

What is the difference between a 502 and a 504?

502 Bad Gateway: Nginx could not talk to the application (process down, wrong port or socket). 504 Gateway Time-out: it reached it but the answer came too late.

I set proxy_read_timeout to 600s and still get a 504, why?

Another layer cuts first: load balancer (60 s by default on AWS ALB), Cloudflare (error 524 after 100 s), or the directive is in the wrong block. For PHP-FPM, fastcgi_read_timeout is what counts, not proxy_read_timeout.

The 504 only happens at peak hours, what should I do?

That is a sign of saturation: requests wait for a free worker. Profile the most called routes, add caching and workers, or spread the load over several instances.

Useful tools for this error

More DevOps, servers & cloud errors

All DevOps, servers & cloud errors →