Home  >  Article  >  Backend Development  >  Remember nginx 504 Gateway Time-out, nginxtime-out_PHP tutorial

Remember nginx 504 Gateway Time-out, nginxtime-out_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:411200browse

Remember once nginx 504 Gateway Time-out, nginxtime-out

When the program was executing an excel export task today, an nginx timeout prompt appeared

nginx 504 Gateway Time-out

Troubleshooting process:

View the task and find that the content is a data volume of 20,000 pieces of information. Each piece of information has 50 fields. This problem occurred when exporting to excel

The execution time is about 10 minutes before a timeout occurs

Analysis:

Nginx 504 Gateway Time-out means that the requested gateway has not been requested. Simply put, it means that no executable PHP-CGI has been requested
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. When creating a connection such as a database, the connection cannot be connected for some reason, and there is no mechanism for timeout failure, resulting in the connection 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

Check the relevant configurations of nginx and php respectively to view the specific parameters of timeout

Find 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, I ran it again and found that there was no 504 timeout prompt, but the page was blank and no files were exported. It should still be that php times out during execution

There is no way to check the php code and find that there is an execution time setting: set_time_limit

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

set_time_limit

This function is used to configure the longest execution time of the 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, add this line in the php function:

set_time_limit(0);

Execute it again and the result is OK.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1116072.htmlTechArticleRemember nginx 504 Gateway Time-out, nginxtime-out Today the program appeared when executing an excel export task Got nginx timeout prompt nginx 504 Gateway Time-out troubleshooting process: Check...
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