使用 PHP set_time_limit() 防止 nginx 504 网关超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16002268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent nginx 504 Gateway timeout using PHP set_time_limit()
提问by Nyxynyx
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?
当我的 PHP 脚本运行时间比平时长时,我从 nginx 收到 504 超时消息。set_time_limit(0)
似乎并没有阻止!在 nginx 上运行 php5-fpm 时它不起作用吗?如果是这样,设置时间限制的正确方法是什么?
Error:
错误:
504 Gateway Time-out
nginx/1.2.7
回答by pymkin
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:
有几种方法可以设置 php-fpm 的超时时间。在/etc/php5/fpm/pool.d/www.conf
我添加了这一行:
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:
另外,在/etc/nginx/sites-available/default
我将以下行添加到相关服务器的位置块中:
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.
现在只需重新启动 php-fpm 和 nginx,并且不应该有少于 180 秒的请求超时。
回答by arp
Try this link, it has a better solution on how to fix this. So the steps are:
试试这个链接,它有一个更好的解决方案来解决这个问题。所以步骤是:
- Open your
nginx.conf
file located in/etc/nginx
directory. Add this below piece of code under
http {
section:client_header_timeout 3000; client_body_timeout 3000; fastcgi_read_timeout 3000; client_max_body_size 32m; fastcgi_buffers 8 128k; fastcgi_buffer_size 128k;
Note: If its already present , change the values according.
Reload Nginx and php5-fpm.
$ service nginx reload $ service php5-fpm reload
If the error persists, consider increasing the values.
- 打开
nginx.conf
位于/etc/nginx
目录中的文件。 在
http {
部分下添加以下代码:client_header_timeout 3000; client_body_timeout 3000; fastcgi_read_timeout 3000; client_max_body_size 32m; fastcgi_buffers 8 128k; fastcgi_buffer_size 128k;
注意:如果它已经存在,请根据更改值。
重新加载 Nginx 和 php5-fpm。
$ service nginx reload $ service php5-fpm reload
如果错误仍然存在,请考虑增加值。
回答by Bart
You can't use PHP to prevent a timeout issued by nginx.
您不能使用 PHP 来阻止 nginx 发出的超时。
To configure nginx to allow more time see the proxy_read_timeout
directive.
要配置 nginx 以允许更多时间,请参阅proxy_read_timeout
指令。
回答by tfont
The correct answer is increasing fastcgi_read_timeoutin your Nginx configuration.
Simple as that!
正确答案是在 Nginx 配置中增加fastcgi_read_timeout。
就那么简单!
回答by kabus
sudo nano /etc/nginx/nginx.conf
Add these variables to nginx.conf file:
将这些变量添加到 nginx.conf 文件中:
http {
# .....
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
And then restart:
然后重新启动:
service nginx reload
回答by Gayan Kavirathne
There are three kinds of timeouts which can occur in such a case. It can be seen that each answer is focused on only one aspect of these possibilities. So, I thought to write it down so someone visiting here in future does not need to randomly check each answer and get success without knowing which worked.
在这种情况下可能发生三种超时。可以看出,每个答案都只关注这些可能性的一个方面。所以,我想把它写下来,这样将来来这里的人就不需要在不知道哪个有效的情况下随机检查每个答案并获得成功。
- Timeout the request from requester - Need to set timeout header ( see the header configuration in requesting library)
- Timeout from nginx while making the request( before forwarding to the proxied server) eg: Huge file being uploaded
- Timeout after forwarding to the proxied server, server does not reply back nginx in time. eg: Time consuming scripts running at server
- 来自请求者的请求超时 - 需要设置超时标头(请参阅请求库中的标头配置)
- 发出请求时从nginx超时(在转发到代理服务器之前)例如:正在上传巨大的文件
- 转发到代理服务器后超时,服务器没有及时回复nginx。例如:在服务器上运行的耗时脚本
So the fixes for each issue are as follows.
因此,每个问题的修复如下。
- set timeout header eg: in ajax
- 设置超时标头,例如:在 ajax 中
$.ajax({
url: "test.html",
error: function(){
// will fire when timeout is reached
},
success: function(){
//do something
},
timeout: 3000 // sets timeout to 3 seconds
});
nginx Client timeout
http{ #in seconds fastcgi_read_timeout 600; client_header_timeout 600; client_body_timeout 600; }
nginx proxied server timeout
http{ #Time to wait for the replying server proxy_read_timeout 600s; }
nginx 客户端超时
http{ #in seconds fastcgi_read_timeout 600; client_header_timeout 600; client_body_timeout 600; }
nginx 代理服务器超时
http{ #Time to wait for the replying server proxy_read_timeout 600s; }
So use the one that you need. Maybe in some cases, you need all these configurations. I needed.
因此,请使用您需要的那个。也许在某些情况下,您需要所有这些配置。我需要。
回答by kenorb
You need to add extra nginx directive (for ngx_http_proxy_module
) in nginx.conf
, e.g.:
您需要在 中添加额外的 nginx 指令(for ngx_http_proxy_module
)nginx.conf
,例如:
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.
基本上 nginxproxy_read_timeout
指令会更改代理超时,这FcgidIOTimeout
适用于安静时间过长FcgidBusyTimeout
的脚本,以及执行时间过长的脚本。
Also if you're using FastCGI application, increase these options as well:
此外,如果您正在使用 FastCGI 应用程序,请增加这些选项:
FcgidBusyTimeout 300
FcgidIOTimeout 250
Then reload nginx and PHP5-FPM.
然后重新加载 nginx 和 PHP5-FPM。
Plesk
普莱斯克
In Plesk, you can add it in Web Server Settingsunder Additional nginx directives.
在 Plesk 中,您可以在其他 nginx 指令下的Web 服务器设置中添加它。
For FastCGI check in Web Server Settingsunder Additional directives for HTTP.
对于 FastCGI,请检查“ HTTP 的附加指令”下的“ Web 服务器设置”。
回答by Kate
Since you're using php-fpm you should take advantage of fastcgi_finish_request() for processing requests you know can take longer.
由于您使用的是 php-fpm,因此您应该利用 fastcgi_finish_request() 来处理您知道可能需要更长时间的请求。
回答by pangkalizer
Using set_time_limit(0)
is useless when using php-fpm or similar process manager.
使用set_time_limit(0)
php-fpm 或类似的进程管理器时使用是无用的。
Bottomline is not to use set_time_limit
when using php-fpm
, to increase your execution timeout, check this tutorial.
底线是在使用set_time_limit
时不要使用php-fpm
,要增加执行超时,请查看本教程。
回答by NeuroZ
I solve this trouble with config APACHE ! All methods (in this topic) is incorrect for me... Then I try chanche apache config:
我用配置 APACHE 解决了这个问题!所有方法(在本主题中)对我来说都不正确......然后我尝试chanche apache config:
Timeout 3600
Timeout 3600
Then my script worked!
然后我的脚本工作了!