Prevent nginx 504 Gateway timeout using PHP set_ti

2019-01-08 04:24发布

I am getting 504 timeouts message from nginx when my PHP script is running longer than usual. set_time_limit(0) does not seem to prevent that! Does it not work when running php5-fpm on nginx? If so, whats the proper way of setting the time limit?

Error:

504 Gateway Time-out
nginx/1.2.7

9条回答
何必那么认真
2楼-- · 2019-01-08 04:57

Using set_time_limit(0) is useless when using php-fpm or similar process manager.

Bottomline is not to use set_time_limit when using php-fpm, to increase your execution timeout, check this tutorial.

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-08 05:00

You need to add extra nginx directive (for ngx_http_proxy_module) in nginx.conf, e.g.:

proxy_read_timeout 300;

Basically the nginx proxy_read_timeout directive changes the proxy timeout, the FcgidIOTimeout is for scripts that are quiet too long, and FcgidBusyTimeout is for scripts that take too long to execute.

Also if you're using FastCGI application, increase these options as well:

FcgidBusyTimeout 300
FcgidIOTimeout 250

Then reload nginx and PHP5-FPM.

Plesk

In Plesk, you can add it in Web Server Settings under Additional nginx directives.

For FastCGI check in Web Server Settings under Additional directives for HTTP.

See: How to fix FastCGI timeout issues in Plesk?

查看更多
家丑人穷心不美
4楼-- · 2019-01-08 05:01

The correct answer is increasing fastcgi_read_timeout in your Nginx configuration.
Simple as that!

查看更多
Evening l夕情丶
5楼-- · 2019-01-08 05:04

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.

查看更多
萌系小妹纸
6楼-- · 2019-01-08 05:04
 sudo nano /etc/nginx/nginx.conf

Add these variables to nginx.conf file:

http {  
  # .....
  proxy_connect_timeout       600;
  proxy_send_timeout          600;
  proxy_read_timeout          600;
  send_timeout                600;
}

And then restart:

service nginx reload
查看更多
欢心
7楼-- · 2019-01-08 05:18

Since you're using php-fpm you should take advantage of fastcgi_finish_request() for processing requests you know can take longer.

查看更多
登录 后发表回答