Nginx 502 and 504 wrong solutions

Source: Internet
Author: User
Tags fpm php script time limit

First, the error hint Description:

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.

Second, the cause of error analysis:

To solve these two problems is a need for comprehensive thinking, in general Nginx 502 Bad Gateway and php-fpm.conf settings,

and the Nginx 504 Gateway time-out is related to the nginx.conf setting.

Php-fpm.conf has two critical parameters, one is "Max_children" and the other is "request_terminate_timeout", but the 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 be suspended, 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 is in principle 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.

III. Interim Solutions:

To sum up, the Nginx tips for 502 and 504 errors in the interim solution are:

1, adjust the relevant settings of php-fpm.conf:

<value name= "Max_children" >32</value>

<value name= "Request_terminate_timeout" >30s</value>

2, adjust the relevant settings of nginx.conf:

    Server_tokens off;
    sendfile        on;
    tcp_nopush     on;
    server_names_hash_bucket_size 256;
    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;
    client_body_buffer_size 256k;
    client_header_timeout     3m;
    client_body_timeout 3m;
    send_timeout              3m;
    client_max_body_size 50m;
    keepalive_timeout ;

#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k;

Fastcgi_intercept_errors on;
Fastcgi_connect_timeout 600;
Fastcgi_send_timeout 600;
Fastcgi_read_timeout 600;
Fastcgi_buffer_size 256k;
Fastcgi_buffers 256k;
Fastcgi_busy_buffers_size 512k;
Fastcgi_temp_file_write_size 512k;

IV, Solution:
A, crontab write php-fpm Smooth restart command
B, appropriately increase the children value, reduce the max_requests value.
     recommends General cloud host children in 5-10,max_requests at 1000-2000, specifically self tuning. &NBSP
    , reducing the number of max_requests can shorten the cycle of php-cgi restart
     Accidental high memory operations can cause problems that affect time.

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.