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.
Answer from pymkin on Stack OverflowThere 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:
- Open your
nginx.conffile located in/etc/nginxdirectory. 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.
Reload Nginx and php5-fpm.
$ service nginx reload $ service php5-fpm reloadIf the error persists, consider increasing the values.
504 Gateway Timeout Error Due To PHP 7.4 FPM API
timeout - 504 Gateway Time-out Error on PHP Script - Stack Overflow
Error: 504 Gateway Timeout after 60 seconds. | OpenLiteSpeed Community and News
504 Gateway Timeout appears after 60 seconds even though PHP_MAX_EXECUTION_TIME is set
Script will not stop executing until they reach php timeout itself.
The 504 error are generated on the gateway / proxy itself, it's not coming from the php process.
It's because this, if you have an Apache with Apache-mod-php you will never receive this error, because there's not a proxy.
Extending the explanation, think as follows:
You have a PHP process. The PHP process can be a PHP-FPM, PHP-CGI, or Apache-MOD-PHP. On this process, you have a timeout (configured on php.ini or with a ini_set).
The PHP proxy gives a response in the allowed time (aka: if you have a set_time_limit(600), your PHP process can be running up to 10 minutes).
Without relation on this sentence, you can have another process waiting for these response: That's the case of a apache (configured to contact to php by cgi or fpm), a nginx, a lighthttpd, and others. That's not the case of a apache configured by apache-mod-php. This second process, have a new timeout (proxy_timeout), configured on the vhost / general server app config. That's the time the program will wait for a response from PHP processing engine.
The last sentence, can be repeated on each proxy / gateway.
Think on this scenario:
haproxy (Timeout 1) --> Nginx (Frontend / cache) (Timeout 2) --> Apache (Timeout 3) -> PHP-FPM (PHP Timeout / set_time_limit).
And a very simple scenario:
Apache (with apache-mod-php) (PHP Timeout / set_time_limit).
Each timeout appearance (except the PHP Timeout itself) is a probable origin of a 504 HTTP gateway timeout error.
My understanding is that the script does continue running, but stops reporting data to the client. (Not that I have the knowledge to explain it.) Here's an article that may help:
How to Fix a 504 Gateway Timeout Error in WordPress
So guys we were using php 7.4 fpm for api and we're getting 504 Gateway timeout. so i increased the memory_limit from 128M - 512M which is believe is 512 Megabytes, also increased max_execution_time from 30 to 300 which i believe is 300 seconds in /etc/php/7.4/fpm/php.ini file.
but before doing that I stopped the php7.4-fpm server and when the changes done, i started it. The below php7.4-fpm error logs are before stopping the fpm and after starting the fpm.
php7.4-fpm error log:
[30-Dec-2022 03:22:38] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[30-Dec-2022 03:26:45] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[30-Dec-2022 03:27:28] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[30-Dec-2022 06:24:58] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
[30-Dec-2022 07:13:10] NOTICE: Terminating ...
[30-Dec-2022 07:13:10] NOTICE: exiting, bye-bye!
[30-Dec-2022 07:24:11] NOTICE: fpm is running, pid 1146754
[30-Dec-2022 07:24:11] NOTICE: ready to handle connections
[30-Dec-2022 07:24:11] NOTICE: systemd monitor interval set to 10000ms
[30-Dec-2022 09:39:59] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
as i can see, the api is working fine now but after restarted we can see these is this pm.max_children error still occurred once. It has been 4 hours and right now while writing this these is still only that 1 error after started the fpm service. can anybody please help me regarding the error. I once tried to change these values before but it ended up breaking the php-fpm service and it was not starting at all so had to revert back the changes made to the pm.max_children parameters.
Thanks.
You can increase maximum execution time in php.
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Write an info file on your server containing
<?php phpinfo();
View the file in your browser and check the max_execution_time

If it's 0 then there is something different to do else make sure you changed on correct ini file.
You can also check your ini file location

If you still can't fix it please reply :)
Increasing the max_execution_time limit in php.ini
max_execution_time=600
Increase the default timeout value in the httpd.conf
Timeout 600
At the beginning of the script, start displaying information, for example
print " ";
continue to send data periodically to maintain the connection
Also, check your firewall settings. I was having this problem, where the PHP code was using PHPMailer to send out an email on a certain action (delete a user). The email was going to mailtrap.io, they had changed some settings at their end, my firewall rule was too restrictive, and the outbound sendmail connection would hang up PHP and cause a timeout. I spent days looking for this problem...
I encountered the same problem and I used ini_set('default_socket_timeout', 6000); to fix it.
http://php.net/manual/en/filesystem.configuration.php#ini.default-socket-timeout
"Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."
This would indicate something is not configured properly on the server.
Can't follow why you think this is a CloudFlare issue right now (from the tag). Are you getting a CloudFlare error message at all?