使用 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;
整个位置块看起来像这样:
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;
}
现在只需重新启动 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屋!