9

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

Jan Richter
  • 1,976
  • 4
  • 29
  • 49
  • Maybe you have an error in your php code. We resolve similar issue like this answer by fixing our php code: https://stackoverflow.com/questions/26070299/nginx-php-fpm-504-timeout-error-upstream-timed-out-110-connection-timed-o – Abdualiym Jul 05 '20 at 12:20

1 Answers1

24

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;
Jan Richter
  • 1,976
  • 4
  • 29
  • 49
  • In what file did you end up putting these changes? – inspirednz Jan 14 '21 at 23:19
  • 3
    I believe it was `/etc/nginx/sites-enabled/your-site` which contained the configuration. Just make sure there are no overrides from the includes defined later on. – Jan Richter Jan 18 '21 at 01:14
  • This worked for me. Ive putted this configuration inside /conf.d folder of Nginx installation. My environment is an AWS ElasticBeanstalk instance. There is a folder at /etc/nginx/conf.d/elasticbeanstalk. Inside this I created a new file with the new configuration – Martin Muñoz Feb 03 '23 at 12:49