使用PHP set_time_limit()防止Nginx 504网关超时 [英] Prevent nginx 504 Gateway timeout using PHP set_time_limit()
问题描述
当我的PHP脚本运行时间比平时长时,我从nginx收到504超时消息. set_time_limit(0)
似乎不能阻止这种情况!在Nginx上运行php5-fpm时不起作用吗?如果是这样,设置时间限制的正确方法是什么?
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?
错误:
504 Gateway Time-out
nginx/1.2.7
推荐答案
有几种方法可以设置php-fpm的超时时间.在/etc/php5/fpm/pool.d/www.conf
中,我添加了以下行:
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
此外,在/etc/nginx/sites-available/default
中,我在有问题的服务器的位置块中添加了以下行:
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;
整个位置块如下所示:
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;
}
现在,只需重新启动php-fpm和nginx即可,并且请求的时间不会少于180秒.
Now just restart php-fpm and nginx and there should be no more timeouts for requests taking less than 180 seconds.
这篇关于使用PHP set_time_limit()防止Nginx 504网关超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!