Prevent Nginx 504 Gateway Timeout Using PHP Set_Time_Limit()

Nginx PHP set_time_limit() not working

First, you have to edit your Nginx configuration files to change fastcgi_read_timeout. There is no getting around that, you have to change that setting.

I'm not sure why you say "I shouldn't modify these files in my case". I think your reason might be that you want to change the timeout for one of your websites, but not others. The best way I've found to accomplish that is go ahead and set fastcgi_read_timeout to a very long timeout (the longest you would want for any of your sites).

But you won't really be counting on using that timeout, instead let PHP handle the timeouts. Edit your PHP's php.ini and set max_execution_time to a reasonable amount of time that you want to use for most your websites (maybe 30 seconds?).

Now, to use a longer timeout for a particular website or web page, use the set_time_limit() function in PHP at the beginning of any scripts you want to allow to run longer. That's really the only easy way to have a different setting for some websites but not others on a Nginx / PHP-FPM set up. Other ways of changing the timeout are difficult to configure because of the way PHP-FPM shares pools of PHP threads with multiple websites on the server.

How to increase page timeout to prevent 504 error?

fastcgi_read_timeout should be put into a location which you're using for processing requests to your file.

location {
fastcgi_pass you.app:9000;
...
fastcgi_read_timeout 900s; # 15 minutes
}

Please, see more examples in documentation

Nginx + PHP-FPM 7.1 - 504 Gateway Time-out

The question is probably why does your backend take so long to respond? Not sure about your usecase but normally it's not user-friendly to wait to long for a response.

To answer your question:

I found this link: https://easyengine.io/tutorials/php/increase-script-execution-time/

Add in /etc/php5/fpm/php.ini

max_execution_time = 300

Set in /etc/php5/fpm/pool.d/www.conf

request_terminate_timeout = 300

Set in /etc/nginx/nginx.conf

http {
#...
fastcgi_read_timeout 300;
#...
}

And in your config:

location ~ \.php$ { 
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;
}

And reload services

service php5-fpm reload 
service nginx reload

How do I prevent a Gateway Timeout with FastCGI on Nginx

Proxy timeouts are well, for proxies, not for FastCGI...

The directives that affect FastCGI timeouts are client_header_timeout, client_body_timeout and send_timeout.

Edit: Considering what's found on nginx wiki, the send_timeout directive is responsible for setting general timeout of response (which was bit misleading). For FastCGI there's fastcgi_read_timeout which is affecting the FastCGI process response timeout.

nginx/fastcgi 504 gateway error, increasing fastcgi_read_timeout isn't helping

Have you checked the max_execution_time value in php.ini?

That's the only other configurable value I can think of that might be causing a timeout.

PHP-FPM served by Nginx: 504 Gateway Time-out - nginx

Looks like you need to increase fastcgi_read_timeout in your nginx configuration.



Related Topics



Leave a reply



Submit