In 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: example.com, request: "POST /path/to/some/script.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "example.com", referrer: "http://example.com/"
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_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
9 comments
Thanks for this - seems so
Submitted by Phil (not verified) on 18. June 2013 - 12:47Thank you so much
Submitted by boyriot (not verified) on 25. December 2013 - 19:07Thanks man, helped me!
Submitted by Ilya (not verified) on 8. April 2014 - 15:37What about CloudFlare and 524 Error
Submitted by Adriano (not verified) on 3. May 2014 - 5:01Re: What about CloudFlare and 524 Error
Submitted by Sasa Tekovic on 3. May 2014 - 9:50Oh 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
Submitted by Matus (not verified) on 16. March 2016 - 14:36No, you don't have to replace
Submitted by Sasa Tekovic on 17. March 2016 - 20:35No, 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
Submitted by epsx (not verified) on 30. March 2017 - 17:21This is not a way to solve
Submitted by harsh mathur (not verified) on 13. October 2017 - 7:55Add new comment