Perfect solution to Nginx 504 Gateway time-out problem _nginx

Source: Internet
Author: User
Tags fpm php script

Recently built a site with Dedecms a large number of content, there are three columns of content of more than 2000, more than 2,300 two columns in the generation of column list when the 504 Gateway time-out server used is nginx, I do not understand, Server maintenance staff casually found an article on the Internet, modified the Nginx cache settings, regardless of the use, he will ignore, but I can not ignore Ah, can not generate a list page, then the content can not be used?

Download the database to the local, in the local configuration of Nginx, tried a lot of times, are not, and changed to Apache below, more exaggerated, generated 83 pages can not continue, it seems to be more powerful nginx, although the 504 Gateway time-out, but somehow can be all generated.

I had to find more solutions on the Internet, tried n many times later, finally let me find a useful way, think about the future may have encountered such a problem, the article was copied over here, for later reference it, for the same problem friend is also a help.

The following section is a reference section, I do not understand the technology, in my dedecms5.6 inside useful, others try it.

Nginx 502 Bad Gateway means that the requested php-cgi has been executed, but the php-cgi process is terminated for some reason (typically a problem 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.

To solve these two problems is a need for comprehensive thinking, in general Nginx 502 bad Gateway and php-fpm.conf settings, and Nginx 504 Gateway time-out is related to the settings of nginx.conf.

The correct setting needs to take into account multiple factors such as the performance of the server itself and the number of visitors.

To my current server for example CPU is Ben 1.5G, Memory 1gb,centos system, the visitor is about 50 people at the same time in route.

But the NPC in route needs to request php-cgi to do a lot of information processing, so I set 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 main setting here is the first three, which is

Fastcgi_connect_timeout 300s;

Fastcgi_send_timeout 300s;

Fastcgi_read_timeout 300s;

This set the php-cgi connection, send and read time, 300 seconds enough to use, so my server rarely appear 504 Gateway time-out this error. The key is the php-fpm.conf setting, which leads directly to 502 bad Gateway and 504 Gateway time-out.

Now let's take a closer look at some of the key parameters of php-fpm.conf:

Php-fpm.conf has two critical 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 universal, but it needs to be calculated by itself.

The method of calculation is as follows:

If your server performance is good enough and your broadband resources are sufficient, PHP scripts do not have loops or bugs, you can set the "request_terminate_timeout" to 0s directly. The meaning of 0s is to allow php-cgi to carry on without any time limit. And if you can't do this, which means that your php-cgi may be a bug, or that your broadband is not enough or that other reasons cause your php-cgi to die, then it's recommended that you assign a value to "Request_terminate_timeout". This value can be set according to the performance of your server. Generally the better performance you can set the higher, 20 minutes-30 minutes can be. Because my server PHP script needs to run for a long time, and some may be more than 10 minutes so I set 900 seconds, so that will not cause php-cgi to die and appear 502 Bad Gateway this error.

And how is the value of "Max_children" calculated? This value in principle is the bigger the better, the php-cgi process will be processed more quickly, the queue of requests will be very little. Set "Max_children" also need to set according to the performance of the server, generally a server under normal circumstances of each php-cgi memory consumed in about 20M, so my "Max_children" I set to 40, 20m*40= 800M means that at peak time all php-cgi are consumed within 800M, less than my valid memory 1Gb. And if my "max_children" set smaller, such as 5-10, then php-cgi will be "very tired", processing speed is also very slow, waiting for a long time. If a long time did not get processing requests will appear 504 Gateway time-out this error, and is processing a very tired of those php-cgi if encountered problems will appear 502 bad Gateway this error.

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.