[php] Prevent nginx 504 Gateway timeout using PHP set_time_limit()

I am getting 504 timeouts message from nginx when my PHP script is running longer than usual. set_time_limit(0) does not seem to prevent that! Does it not work when running php5-fpm on nginx? If so, whats the proper way of setting the time limit?

Error:

504 Gateway Time-out
nginx/1.2.7

This question is related to php nginx fastcgi

The answer is


The correct answer is increasing fastcgi_read_timeout in your Nginx configuration.
Simple as that!


 sudo nano /etc/nginx/nginx.conf

Add these variables to nginx.conf file:

http {  
  # .....
  proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;
}

And then restart:

service nginx reload

You need to add extra nginx directive (for ngx_http_proxy_module) in nginx.conf, e.g.:

proxy_read_timeout 300;

Basically the nginx proxy_read_timeout directive changes the proxy timeout, the FcgidIOTimeout is for scripts that are quiet too long, and FcgidBusyTimeout is for scripts that take too long to execute.

Also if you're using FastCGI application, increase these options as well:

FcgidBusyTimeout 300
FcgidIOTimeout 250

Then reload nginx and PHP5-FPM.

Plesk

In Plesk, you can add it in Web Server Settings under Additional nginx directives.

For FastCGI check in Web Server Settings under Additional directives for HTTP.

See: How to fix FastCGI timeout issues in Plesk?


I solve this trouble with config APACHE ! All methods (in this topic) is incorrect for me... Then I try chanche apache config:

Timeout 3600

Then my script worked!


Using set_time_limit(0) is useless when using php-fpm or similar process manager.

Bottomline is not to use set_time_limit when using php-fpm, to increase your execution timeout, check this tutorial.


There are several ways in which you can set the timeout for php-fpm. In /etc/php5/fpm/pool.d/www.conf I added this line:

request_terminate_timeout = 180

Also, in /etc/nginx/sites-available/default I added the following line to the location block of the server in question:

fastcgi_read_timeout 180;

The entire location block looks like this:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 180;
    include fastcgi_params;
} 

Now just restart php-fpm and nginx and there should be no more timeouts for requests taking less than 180 seconds.


Try this link, it has a better solution on how to fix this. So the steps are:

  1. Open your nginx.conf file located in /etc/nginx directory.
  2. Add this below piece of code under http { section:

    client_header_timeout 3000;
    client_body_timeout 3000;
    fastcgi_read_timeout 3000;
    client_max_body_size 32m;
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 128k;
    

    Note: If its already present , change the values according.

  3. Reload Nginx and php5-fpm.

    $ service nginx reload
    $ service php5-fpm reload
    

    If the error persists, consider increasing the values.


There are three kinds of timeouts which can occur in such a case. It can be seen that each answer is focused on only one aspect of these possibilities. So, I thought to write it down so someone visiting here in future does not need to randomly check each answer and get success without knowing which worked.

  1. Timeout the request from requester - Need to set timeout header ( see the header configuration in requesting library)
  2. Timeout from nginx while making the request ( before forwarding to the proxied server) eg: Huge file being uploaded
  3. Timeout after forwarding to the proxied server, server does not reply back nginx in time. eg: Time consuming scripts running at server

So the fixes for each issue are as follows.

  1. set timeout header eg: in ajax

_x000D_
_x000D_
$.ajax({_x000D_
    url: "test.html",_x000D_
    error: function(){_x000D_
        // will fire when timeout is reached_x000D_
    },_x000D_
    success: function(){_x000D_
        //do something_x000D_
    },_x000D_
    timeout: 3000 // sets timeout to 3 seconds_x000D_
});
_x000D_
_x000D_
_x000D_

  1. nginx Client timeout

    http{
         #in seconds
        fastcgi_read_timeout 600;
        client_header_timeout 600;
        client_body_timeout 600;
     }
    
  2. nginx proxied server timeout

    http{
      #Time to wait for the replying server
       proxy_read_timeout 600s;
    
    }
    

So use the one that you need. Maybe in some cases, you need all these configurations. I needed.


You can't use PHP to prevent a timeout issued by nginx.

To configure nginx to allow more time see the proxy_read_timeout directive.


Since you're using php-fpm you should take advantage of fastcgi_finish_request() for processing requests you know can take longer.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to nginx

Kubernetes service external ip pending nginx: [emerg] "server" directive is not allowed here Disable nginx cache for JavaScript files Nginx upstream prematurely closed connection while reading response header from upstream, for large requests Nginx: Job for nginx.service failed because the control process exited How can I have same rule for two locations in NGINX config? How to verify if nginx is running or not? Find nginx version? Docker Networking - nginx: [emerg] host not found in upstream How do I rewrite URLs in a proxy response in NGINX

Examples related to fastcgi

Nginx serves .php files as downloads, instead of executing them upstream sent too big header while reading response header from upstream nginx: connect() failed (111: Connection refused) while connecting to upstream Prevent nginx 504 Gateway timeout using PHP set_time_limit() What is the difference between fastcgi and fpm? nginx 502 bad gateway Where can I find the error logs of nginx, using FastCGI and Django? How do I prevent a Gateway Timeout with FastCGI on Nginx