Remember one time Nginx 504 Gateway time-out

Source: Internet
Author: User

Today the program in the execution of an Excel export task, there is an Nginx timeout prompt

Nginx 504 Gateway Time-out

Troubleshooting process:

View the task discovery content is a data volume 20,000 information each piece of information has 50 fields when you perform the export to Excel, the problem occurs

Execution time expires in about 10 minutes


The implication of Nginx 504 Gateway time-out is that the requested gateway is not requested, simply that there is no request to be executed php-cgi
There are usually several situations that can cause this problem:
1. The program is dealing with a lot of data, or there are dead loops and other problems
2. Create a connection such as a database because there are some reasons why the connection does not occur, and then there is a mechanism for the time-out failure
3. There are some HTTP requests in the program, these requests take too long to execute, resulting in a time-out

See the relevant configuration of Nginx and PHP separately to see timeout specific parameters

Find Nginx configuration file

#修改Nginx配置: Fastcgi_connect_timeout 1200s; #原设置为300sfastcgi_send_timeout 1200s; #原设置为300sfastcgi_read_timeout 1200s; #原设置为300s  4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 256k;

The most important setting here is the first three, which is

Fastcgi_connect_timeout #同 The connection time-out for the FASTCGI server, the default value is 60 seconds, it cannot exceed 75 seconds;
Fastcgi_send_timeout #Nginx process sends request to the FASTCGI process, the entire process time-out, the default value of 60 seconds;
Fastcgi_read_timeout #FastCGI process to the Nginx process to send response, the entire process time-out, the default value of 60 seconds;

PHP configuration file

Max_execution_time = 300s; The maximum execution time of a PHP script, however, in php-cgi (PHP-FPM), this parameter does not work.
request_terminate_timeout = 0; #设置单个请求的超时中止时间. Set to 0 to continue execution until the end of the program does not time out

After modifying the above settings again, the discovery did not appear 504 timeout prompt, but the page blank, and no file export. Should or should PHP be timed out during execution

There is no way to view the PHP code and do not find the execution time setting is:set_time_limit

Baidu a bit PHP function execution time to find the following content:


This function is used to configure the longest execution time of the page. The default value is 30 seconds, and the max_execution_time variable configuration in php.ini, if configured to 0, is not limited to the longest time.

The calculation is only started when the function is executed. For example, if the default is 30 seconds, and 25 seconds have been executed before the function is executed, and the function is changed to 20 seconds, the maximum execution time for the page is 45 seconds.

Finally, add this line to the PHP function:

Set_time_limit (0);

Once again, the result can be done.

Remember one time Nginx 504 Gateway time-out

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.