Nginx Tip 502 and 504 Error Final Solution

Source: Internet
Author: User
Tags fpm 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 "Max_children" This value is how to calculate out of it. 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:

32

505

2, adjust the relevant settings of nginx.conf:

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. End-Level solutions:

The solution shown in heading 3 can only temporarily solve the problem, and if the site's access is really very large, and nginx+fastcgi can only deal with instantaneous or short time high concurrency has a good effect, so the only ultimate solution is: Timed smooth restart php-cgi.

The specific configuration is as follows:

1. Write a very simple script:

#vi/home/www/scripts/php-fpm.sh

The contents are as follows:

#!/bin/bash
# This script run at */1
/USR/LOCAL/PHP/SBIN/PHP-FPM Reload

2, add the script to the scheduled task:

#crontab-E

The contents are as follows:

*/1 * * * */home/www/scripts/php-fpm.sh

Note: For the sake of convenience, you can write the PHP-FPM smooth restart command directly in the crontab without writing a script.

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.