Home  >  Article  >  Backend Development  >  Remember once nginx 504 Gateway Time-out 504 time out nginx 504 gateway 504 gateway

Remember once nginx 504 Gateway Time-out 504 time out nginx 504 gateway 504 gateway

WBOY
WBOYOriginal
2016-07-29 08:55:191792browse

Today, when the program was executing an excel export task, an nginx timeout message appeared.

nginx 504 Gateway Time-out

Troubleshooting process:

Viewing the task, it was found that the content was a data volume of 20,000 pieces of information. Each piece of information had 50 This problem occurred when a field was exported to excel

The execution time was about 10 minutes before a timeout occurred

Analysis:

Nginx 504 Gateway Time-out means that the requested gateway was not requested. To put it simply It means that there is no request for PHP-CGI that can be executed. Usually the following situations will cause this problem:
1. The program is processing a large amount of data, or there are problems such as infinite loops
2. Creating a connection such as a database because The connection cannot be connected for some reasons, and there is no mechanism for timeout failure, resulting in connections being created all the time
3. There are some http requests in the program, and the execution time of these requests is too long, resulting in timeout

View the relevant configurations of nginx and php respectively to view the timeout Specific parameters

Find the nginx configuration file

<span>#修改Nginx配置:
fastcgi_connect_timeout 1200s;#原设置为300s
fastcgi_send_timeout 1200s;<span>#原设置为300s</span>fastcgi_read_timeout 1200s;<span>#原设置为300s</span>fastcgi_buffer_size 64k;
fastcgi_buffers </span><span>4</span><span> 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;</span>

The most important settings here are the first three, namely

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

php configuration file

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

After modifying the above settings, execute it again and find that there is no 504 timeout prompt. But the page is blank and no file is exported. It should be that php times out during execution

There is no way to check the php code and there is no execution time setting:

set_time_limit

Baidu searched the php function execution time and found the following:

set_time_limit

This function is used Configure the maximum execution time of this page. The default value is 30 seconds, configured in the max_execution_time variable in php.ini. If configured to 0, the maximum time is not limited.

The calculation starts when this function is executed. For example, if the default is 30 seconds, and 25 seconds have been executed before this function is executed, and this function is used to change it to 20 seconds, the maximum execution time of the page will be 45 seconds.

Finally, I added this line to the php function:

set_time_limit(0);

Execute it again, and the result is OK.

The above is an introduction to remembering nginx 504 Gateway Time-out, including the content of 504 gateway time-out. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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