Nginx + Php-fpm fastcgi upstream timed out

11,487

As it happens in these cases, I was actually editing a wrong configuration file that didn't get loaded by Nginx.

Adding the following to the right file did the trick:

fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_connect_timeout 600;
Share:
11,487
Jan Richter
Author by

Jan Richter

I love solving complex problems using AWS.

Updated on June 17, 2022

Comments

  • Jan Richter
    Jan Richter almost 2 years

    I am having issues with a long-running PHP script:

    <?php
    sleep(70); # extend 60s
    phpinfo();
    

    Which gets terminated every time after 60 seconds with a response 504 Gateway Time-out from Nginx.

    When I inspect the Nginx errors I can see that the request times out:

    ... [error] 1312#1312: *2023 upstream timed out (110: Connection timed out) while reading response header from upstream, ... , upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock", ...
    

    I went through the related questions and tried increasing the timeouts creating a /etc/nginx/conf.d/timeout.conf file with the following content:

    proxy_connect_timeout 600;
    proxy_send_timeout 600;
    proxy_read_timeout 600;
    send_timeout 600;
    fastcgi_read_timeout 600;
    fastcgi_send_timeout 600;
    fastcgi_connect_timeout 600;
    

    I also read through the Nginx documentation for both fastcgi and core modules, searching for any configurations with defaults set to 60 seconds.

    I ruled out the client_* timeouts because they return HTTP 408 instead of HTTP 504 responses.

    This is my Nginx server config portion of FastCGI:

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include fastcgi_params;
    }
    

    From what I read so far this doesn't seem to be an issue with PHP rather Nginx is to blame for the timeout. Nonetheless, I tried modifying the limits in PHP as well:

    My values from the phpinfo():

    default_socket_timeout=600
    max_execution_time=300
    max_input_time=-1
    memory_limit=512M
    

    The php-fpm pool config also has the following enabled:

    catch_workers_output = yes
    request_terminate_timeout = 600
    

    There is nothing in the php-fpm logs.

    I am also using Amazon's Load Balancer to route to the server, but the timeout configuration is also increased from the default 60 seconds.

    I don't know where else to look, during all the changes I restarted both php-fpm and nginx.

    Thank you