Solution to Nginx 504 gateway timeout error

Source: Internet
Author: User
Tags fpm php script time limit vps nginx server


Recently, we need to use the Excel import function. Because there are a lot of Excel data, and our server programs need to verify the data content, many external service interfaces will be called, so there is no suspense that the Excel import interface has been called for more than one minute and the error 504 gateway timeout is reported. There are two solutions:

1. Optimize business code

If an interface is called for more than one minute, it must be optimized to check whether the database or interface call is reasonable and whether the request can be merged.

2. Modify the Nginx server configuration

If the optimization fails, you can increase the Nginx timeout.
Check whether the time meets the requirements. The three parameters in nginx. config are as follows:

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;

The unit is second.

If Nginx proxy is used, you can add:

Proxy_connect_timeout 300 seconds;
Proxy_send_timeout 300 s;
Proxy_read_timeout 300 seconds;

To:

Location/foo {
Proxy_pass http://xxx.xxx.xxx.xxx: 8080/foo;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_connect_timeout 300 seconds;
Proxy_send_timeout 300 s;
Proxy_read_timeout 300 seconds;
Access_log/var/log/nginx/access. foo. log main;
Error_log/var/log/nginx/error. foo. log;
}

If the problem persists, let's take a look.

From the error code, it can be basically determined that it has nothing to do with nginx, mainly because the request submitted to php-fpm fails to provide correct feedback. Generally, when submitting a dynamic request, nginx will directly forward the request to php-fpm, while php-fpm will distribute the php-cgi process to process the related requests, and then return them in sequence, finally, nginx sends the result to the client browser. However, my vps currently runs a pure php application. In fact, all users' requests are php requests, and some of them take a long time, the php-cgi process is always full, and the configuration file of php-fpm itself only opens 10 php-cgi processes, in this way, if there are a few online users, the requests will not be processed properly and errors will occur.
 
After analyzing the original cause, it is easier to do the following: first, modify the configuration of php-fpm:
 
Change max_children from the previous 10 to the current 30, so that sufficient php-cgi processes can be used;
Change request_terminate_timeout from the previous 0 s to 60 s, so that the timeout time for the php-cgi process to process the script is 60 seconds, which can prevent the process from being suspended and improve the utilization efficiency.
 
Then, modify several nginx configuration items to reduce the number of FastCGI requests and try to keep buffers unchanged:
 
Change fastcgi_buffers from 4 64 k to 2 256 k;
Fastcgi_buffer_size is changed from 64 k to 128 K;
Fastcgi_busy_buffers_size is changed from 128 K to 256 K;
Fastcgi_temp_file_write_size is changed from 128 K to 256 K.
 
Okay, reload the configuration of php-fpm and nginx, and test again. So far, No 504 Gateway Time-out has been found in two weeks.

In addition, the default static processing method of php-fpm will make the php-cgi process occupy the memory for a long time and cannot be released, which is also one of the causes of nginx errors, therefore, you can change the php-fpm processing method to the apache mode.
<Value name = "style"> apache-like </value>

Tests from the change to the present show that the above method is still very effective, and no Nginx502 bad gateway or 504 Gateway Time-out error was found. Of course, if your VPS or server performance is good enough, you do not need to make any necessary changes based on the actual situation.


Instance


Taking my current server as an example, the CPU runs for 4 Gbps, with 1 GB of memory and CENTOS. The number of visitors is about 50 online simultaneously.

However, most people online need to request a PHP-CGI for a lot of information processing, so I set nginx. conf:
Fastcgi_connect_timeout 300 s;
Fastcgi_send_timeout 300 s;
Fastcgi_read_timeout 300 s;
Fastcgi_buffer_size 128 k;
Fastcgi_buffers 8 128 k; #8 128
Fastcgi_busy_buffers_size 256 k;
Fastcgi_temp_file_write_size 256 k;
Fastcgi_intercept_errors on;
The most important setting here is the first three, that is
Fastcgi_connect_timeout 300 s;
Fastcgi_send_timeout 300 s;
Fastcgi_read_timeout 300 s;
This specifies the connection, send, and read Time for the PHP-CGI, 300 seconds is enough, so my server rarely gets the 504 Gateway Time-out error. The most critical is the php-fpm.conf settings, which directly results in 502 Bad Gateway and 504 Gateway Time-out.

Next we will carefully analyze several important parameters of the php-fpm.conf:
The php-fpm.conf has two crucial parameters: "max_children" and "request_terminate_timeout"
One of my two values is "40 & Prime;" and the other is "900 & Prime;", but this value is not general and needs to be calculated by myself.
The calculation method is as follows:
If your server has good performance and sufficient bandwidth resources, you can directly set "request_terminate_timeout" to 0 s if the PHP script does not have loops or bugs. 0 s means to keep the PHP-CGI running without a time limit. And if you can't do that, that is, your PHP-CGI may have a BUG, or if your bandwidth is insufficient or your PHP-CGI may be suspended for other reasons, we recommend that you assign a value to "request_terminate_timeout", which can be set based on the performance of your server. In general, the better the performance, the higher you can set, 20 minutes-30 minutes can be done. Since my server PHP script takes a long time to run, some may take more than 10 minutes, so I set 900 seconds, this will not cause the PHP-CGI to die and the 502 Bad gateway error occurs.
How is the value of "max_children" calculated? In principle, the larger the value is, the better. If the php-cgi process is too large, it will process very quickly and there will be very few requests in the queue. "Max_children" also needs to be set based on the server performance. Generally, a server normally consumes about 20 MB of memory per php-cgi, so my "max_children" I set to 40, 20 M * 40 = 800M that is to say that at the peak of all PHP-CGI consumption exists within M, less than my effective memory 1 Gb. If my "max_children" settings are small, such as 5-10, php-cgi will be "very tired", the processing speed will be slow, and the waiting time will be long. If a request is not processed for a long Time, the error 504 Gateway Time-out occurs, if the php-cgi that is working very well encounters a problem, the 502 Bad gateway error will occur.
Fastcgi settings are added to server.
If the prompt is displayed after nginx is started:
Nginx: [emerg] unknown directive "fastcgi_connect_timeout" in/home/chen/workspace/jamy/nginx. conf: 2
An error similar to this may be that the front space is not removed.

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: info-contact@alibabacloud.com 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.