Prevent nginx 504 Gateway timeout using PHP set_time_limit()

You can use the set_time_limit() function in PHP to increase the maximum execution time of a PHP script. This can help to prevent a 504 Gateway Timeout error in cases where the script is taking too long to execute and the server is timing out the request.

Here is an example of how to use set_time_limit():

set_time_limit(30); // Set the maximum execution time to 30 seconds

// Your PHP code goes here

Watch a course Learn object oriented PHP

Keep in mind that set_time_limit() only affects the execution time of the PHP script, not the time it takes for the server to receive and process the request. If the server is taking a long time to process the request, you may need to look at other factors, such as the server's resource utilization or the performance of the application.

You can also adjust the fastcgi_read_timeout directive in the nginx.conf file to increase the amount of time that nginx will wait for a response from the PHP-FPM process. This can help to prevent 504 Gateway Timeout errors in cases where the PHP script is taking a long time to execute.

fastcgi_read_timeout 300; // Set the timeout to 300 seconds (5 minutes)