记一次 nginx 504 Gateway Time-out,nginxtime-out_PHP教程

WBOY
Release: 2016-07-12 08:55:41
Original
1066 people have browsed it

记一次 nginx 504 Gateway Time-out,nginxtime-out

今天程序在执行一项excel导出任务的时候 出现了nginx超时的提示

nginx 504 Gateway Time-out

排查过程:

查看该任务 发现内容是一个数据量20000条信息 每条信息有50个字段 在执行导出为excel的时候 出现了该问题

执行时间大概在10分钟左右就出现超时

分析:

Nginx 504 Gateway Time-out的含义是所请求的网关没有请求到,简单来说就是没有请求到可以执行的PHP-CGI
通常以下几种情况会导致这个问题:
1.程序在处理大量的数据,或者有死循环之类的问题
2.创建数据库之类的连接因为某些原因连接不上,然后没有超时失败的机制,导致一直在创建连接
3.程序中有一些http请求,这些请求执行时间过长,导致超时

分别查看nginx及php的相关配置查看timeout具体参数

找到nginx配置文件

#修改Nginx配置:
fastcgi_connect_timeout 1200s;#原设置为300s
fastcgi_send_timeout 1200s;#原设置为300s
fastcgi_read_timeout 1200s;#原设置为300s
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
Copy after login

这里最主要的设置是前三条,即

fastcgi_connect_timeout #同 FastCGI 服务器的连接超时时间,默认值60秒,它不能超过75秒;
fastcgi_send_timeout #Nginx 进程向 FastCGI 进程发送 request ,整个过程的超时时间,默认值60秒;
fastcgi_read_timeout #FastCGI 进程向 Nginx 进程发送 response ,整个过程的超时时间,默认值60秒;
Copy after login

php配置文件

php.ini
max_execution_time = 300s;PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效。
php-fpm
request_terminate_timeout = 0; #设置单个请求的超时中止时间.设置为0 即一直执行下去直到程序结束 不会超时
Copy after login

修改完上述设置之后 再次执行发现没有出现504超时提示,但是页面空白,并没有文件导出。应该还是php在执行过程中超时

没办法 查看php代码并没有发现有执行时间的设置即:set_time_limit

百度一下php函数执行时间 查到以下内容:

set_time_limit

本函数用来配置该页最久执行时间。默认值是 30 秒,在 php.ini 中的 max_execution_time 变量配置,若配置为 0 则不限定最久时间。

当执行到该函数时,才开始计算。例如,若默认是 30 秒,而在执行到该函数前已执行了 25 秒,而用本函数改为 20 秒,则该页面最长执行时间为 45 秒。

最后在php函数里 添加了这样一行:

set_time_limit(0);

再次执行,结果可以了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1116072.htmlTechArticle记一次 nginx 504 Gateway Time-out,nginxtime-out 今天程序在执行一项excel导出任务的时候 出现了nginx超时的提示 nginx 504 Gateway Time-out 排查过程: 查...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!