Fixing timeout between Nginx and PHP-FPM

SysadminIn case you've set up Nginx as reverse proxy for PHP-FPM and you have very slow or complex PHP script that executes for quite some time, you've probably seen an 504 gateway time-out error. This is pretty common error, but in most cases people are barking at the wrong tree and they're having difficulties finding out which timeout directive is the right one for their scenario.

I've seen some people desperately experimenting with proxy_read_timeout, send_timeout, even client_header_timeout and client_body_timeout directives, but if they bothered to take a peek into Nginx's error_log, they would've seen an error such as:



2013/01/19 11:36:59 [error] 14564#0: *1215
upstream timed out (110: Connection timed out) while reading response header
from upstream, client: 123.456.789.123, server:, request:
"POST /path/to/some/script.php HTTP/1.1", upstream: "fastcgi://",
host: "", referrer: ""

The error clearly states that connection timed out between Nginx and upstream server - in this case PHP-FPM, but that could be any FastCGI server - while reading response header. If you put 2 and 2 together, you just can't miss the fastcgi_read_timeout directive. Quick glance at the documentation reveals what this directive does:

Directive sets the amount of time for upstream to wait for a fastcgi process to send data. Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate.

So, adding this directive with high enough value to http, server or location context would be a smart move. For example:

location ~* \.php$ {
    include         fastcgi_params;
    fastcgi_index   index.php;
    fastcgi_read_timeout 120;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;



Thanks for this - seems so easy now that I've read it.

Thanks a lot, it works like a charm!@

Thanks man, helped me!

After fixing all the timeouts, everything goes right , but I keep getting this 524 errors from Cloudflare.... every timeI upload something or SEND , POST etc something. someone know why this is happening??

Oh c'mon, even you could've googled that. The first part of your answer is here and the second part is in your server logs.


Hi, sorry for the stupid question but I am new in this. I am experiencing gateway timeout error when uploading files bigger than 500mb or so on. The 'SCRIPT_FILENAME' in the last row of the code needs to be replaced by something from my system? If so could you please tell me what to put there? Thanks

No, you don't have to replace the value of SCRIPT_FILENAME with anything. You can leave it just like it's described in the blog post.


for me worked with fastcgi_read_timeout 1020;

This is not a way to solve the issue, it is just a hack to hide the error messages. The solution should something like that the upstream responses in timely manner (in 60 secs) instead of increasing out fastcgi_read_timeout

Add new comment