Solution 502 and 504 gateway time-out (nginx)

Source: Internet
Author: User

504 gateway time-out problems are common in websites that use nginx as the Web server.

I encountered this problem when upgrading the discuz forum.

In general, this situation may be caused by the small buffer of nginx's default FastCGI process response, which will cause the FastCGI process to be suspended. If your FastCGI service does not handle this suspension well, in the end, 504 gateway time-out may occur.
Today's websites, especially some forums, have a lot of replies and a lot of content. One page even has hundreds of K
The default FastCGI Process Response Buffer is 8 KB. We can set a large value.
In nginx. conf, add:

Fastcgi_buffers 8 128 K

This indicates that the FastCGI buffer is set to 8*128 K.
Of course, if you are performing an immediate operation, you may need to increase the value of the nginx timeout parameter, for example, to 60 seconds:

Send_timeout 60;

I just adjusted the two parameters, and the result is that no timeout is displayed. It can be said that the effect is good, but it may also be due to other reasons. Currently, there are not many information about nginx, many things require long-term experience to produce results. We look forward to your findings!

 

 

 

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process is terminated for some reason (generally because the problem with reading resources) is not completed.
Nginx 504 gateway time-out means that the requested gateway does not have a request. Simply put, it does not have a request for PHP-CGI that can be executed.

To solve these two problems, we need to think about it comprehensively. In general, nginx 502 Bad Gateway is related to the setting of the php-fpm.conf, while nginx 504 gateway time-out is related to the setting of nginx. conf.
Correct settings require consideration of multiple factors, such as the server performance and number of visitors.
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" and the other is "900", but this value is not general, but 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 be more than 10 minutes so I set 900 seconds, this will not cause the PHP-CGI to die and 502 appears
Bad Gateway error.

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 the request is not processed for a long time, 504 will appear.
The gateway time-out error, and the very tired PHP-CGI that is being processed will cause the 502 Bad Gateway error if it encounters a problem.

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.