Solution 502, 504 Gateway time-out (nginx)

Source: Internet
Author: User

504 Gateway time-out issues common to Web sites that use Nginx as a Web server

I met this problem when I was upgrading the Discuz forum.

In general, this situation may be due to the Nginx default fastcgi process response buffer is too small, which will cause the fastcgi process to be suspended, if your fastcgi service is not good for this suspend processing, then it is very likely to cause 504 Gateway Time-out
Now the site, especially some forums have a lot of replies and a lot of content, a page even has hundreds of K
The default fastcgi process response buffer is 8K, we can set the large point
In nginx.conf , join:

Fastcgi_buffers 8 128k

This means that the fastcgi buffer is set to 8x128k
Of course, if you are doing an immediate operation, you may need to make the Nginx timeout parameter larger, for example, set to 60 seconds:

Send_timeout 60;

I just adjusted the two parameters, the result is no longer show that timeout, can say the effect is good, but also may be due to other reasons, the current information about Nginx is not many, many things need long-term experience accumulated only results, look forward to your discovery ha!

Second, Nginx 502 bad Gateway

The implication of Nginx 502 Bad Gateway is that the requested php-cgi has been executed, but the php-cgi process is terminated for some reason (generally, the problem of reading the resource) is not completed.

The implication of Nginx 504 Gateway time-out is that the requested gateway is not requested, simply that there is no request to the php-cgi that can be executed.

Solving these two problems is in fact a need for comprehensive thinking, in general, Nginx 502 Bad Gateway and php-fpm.conf settings related, and nginx 504 Gateway time-out is related to the nginx.conf setting.
The correct setting takes into account multiple factors such as the performance of the server itself and the number of visitors.

With my current server as an example CPU is Ben four 1.5G, Memory 1gb,centos system, the visitor is about 50 people at the same time online.
But most people on the line need to ask php-cgi to do a lot of processing, so I set the nginx.conf to:
Fastcgi_connect_timeout 300s;
Fastcgi_send_timeout 300s;
Fastcgi_read_timeout 300s;
Fastcgi_buffer_size 128k;
Fastcgi_buffers 8 128k; #8 128
Fastcgi_busy_buffers_size 256k;
Fastcgi_temp_file_write_size 256k;
Fastcgi_intercept_errors on;
The most important setting here is the first three, which is
Fastcgi_connect_timeout 300s;
Fastcgi_send_timeout 300s;
Fastcgi_read_timeout 300s;

This specifies the php-cgi connection, send and read time, 300 seconds is sufficient, so my server rarely appears 504 Gateway time-out this error. The most critical is the php-fpm.conf setting, which will lead directly to 502 bad Gateway and 504 Gateway time-out.

Let's take a closer look at some important parameters of php-fpm.conf:
php-fpm.conf has two key parameters, one is "Max_children" and the other is "request_terminate_timeout"

The value of my two settings is "40″, one is" 900″, but this value is not generic, but it needs to be calculated by itself.

The method is calculated as follows:
If your server performance is good enough, and the broadband resources are sufficient, PHP scripts do not have loops or bugs, you can set the "Request_terminate_timeout" directly to 0s. The meaning of 0s is to let php-cgi go on without time limit. And if you can't do this, that means your php-cgi may have a bug, or your broadband is not enough or other causes your php-cgi to be able to feign death then it is recommended that you assign a value to "Request_terminate_timeout". This value can be set based on the performance of your server. Generally, the better the performance, the higher you can set, 20 minutes-30 minutes. Because my server PHP script needs to run for a long time, some may be more than 10 minutes so I set 900 seconds, so that does not cause php-cgi dead and 502 bad gateway this error.

And how does the value of "max_children" be calculated? This value is in principle the bigger the better, the php-cgi process is much faster, the queue requests will be very small. Setting "Max_children" also needs to be based on the performance of the server, in general, a server normally consumes about 20M of memory per php-cgi , so my "Max_children" I set to 40, 20m*40 =800m that is, at peak time all php-cgi are within 800M, lower than my valid memory 1Gb. And if my "Max_children" set the smaller, such as 5-10, then php-cgi will be "very tired", processing speed is also very slow, waiting for a longer time. If the request has not been processed for a long time, the 504 Gateway time-out this error, while the very tired of the php-cgi are dealing with the problem will appear 502 Bad Gateway this error.

Solution 502, 504 Gateway time-out (nginx)

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.