What does 502 bad gateway mean? 502 bad gateway error Solution

Source: Internet
Author: User

You can try to clear the browser cache and access your FTP to see if you can log on

Cause

A server (not necessarily a Web server) serves as a gateway or proxy to meet customer requirements (such as a Web browser or our CheckUpDown robot) to access the requested URL. This server receives an invalid response from the upstream server to fulfill its requirements.
Fixed 502 errors
Generally, this problem is caused by poor communication between IP addresses on backend computers, including websites on Web servers that you may try to access. When analyzing this problem, you should clear the browser cache completely.
If you view this problem on all the websites you try to access when you access the Internet, there are two possibilities:
1) your ISP has a major device fault/overload or
2) If your firewall fails to work properly, the problematic internal internet connection may fail.
In the first case, only your ISP can help you. In the second case, what do you need to solve is to prevent you from accessing the Internet.
If this problem occurs only on some websites you are trying to access, it is likely that one of these websites is faulty or overloaded. Contact the website administrator.

A general explanation of the solution to 502 bad getway errors

1. What is 502 bad getway error?
In short, 502 is the gateway with the error type code bad getway.
2. Cause of error
When the connection times out, we send a request to the server. Because there are too many connections to the server, the server cannot provide a normal response, resulting in such an error.
3. Solutions
The best solution is to do it on the server, which is unlikely for everyone.
So what are our solutions?
To put it bluntly, it's easy.
-Refresh (not a general refresh)
Refresh principle: many people may not know that there are two types of refresh.
The so-called refresh is actually to download data from the server to the local hard disk browser,
Then read the data from the local hard disk and show it to the browser.
① Basic Refresh: Click Refresh or use the F5 shortcut key.
Basic refresh only retrieves data from the local hard disk to the browser and does not send a new request to the server.
Most users refresh this frequently and report 502 errors without any effect.
② Refresh from the server: If you click the link of the web page you want to browse again, you will find that the 502 bad getway page is still displayed and can be viewed again now!
Why? When you click the webpage link you want to browse, the data will be re-downloaded from the server.
The solution is to refresh the server: press ctrl + F5 to send a new request to the server.
If the server can respond to you normally, you can see the page.

In-depth analysis of Nginx 502 Bad Gateway and Nginx 504 Gateway Time-out and Solutions

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 generic and 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 take more than 10 minutes, so I set 900 seconds, this will not cause the PHP-CGI to die and the 502 Bad gateway error occurs.
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. You also need to set "max_children" based on the performance of the server. 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 very slow, and the waiting time will be long. If a request is not processed for a long Time, the error 504 Gateway Time-out occurs, if the php-cgi that is working very well encounters a problem, the 502 Bad gateway error will occur.

Nginx 502 bad gateway error Solution

When using Nginx as a Web server, you may encounter an Nginx 502 bad gateway error. There are many reasons for this error. Next we will analyze them one by one.

1. Check whether php-cgi is running
Sometimes php-cgi is directly down due to excessive website traffic or other reasons, so we have to check whether php-cgi is running. Run the following command:

Ps-A | grep php5-cgi
If it is not running, start it manually

/Etc/init. d/php_cgi start
If you find that php-cgi is down for unknown reasons, you can use the following script to temporarily solve this problem and add it to cronjob.

If ps aux | grep 'php5-cgi '| grep-v grep>/dev/null; then echo "PHP-cgi is runnning! "Else echo" PHP-cgi is down. Starting over... "/Etc/init. d/php-fcgi start fi II. Reasons for insufficient fastcgi processes and long php Execution time
The number of fastcgi processes can modify the value of max_children in the php-fpm.conf, the maximum memory consumption of php-cgi during peak hours is 20 M, Please calculate according to your memory.
Limiting php Execution time can be set in request_terminate_timeout in the php-fpm.conf to prevent php-cgi from being suspended due to bugs in the php program.

Iii. FastCGI execution time is too long
Increase the following parameter values based on actual conditions:

Fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;
In addition to the three situations listed above, there are certainly other reasons, but the above three situations are the most common

In the past few days, it has been found that the traffic on the servers of the China Netcom line is unstable. The specific performance is that the traffic is sometimes high or low, and the system load is small when the traffic is low, it was almost 0, but after a while, the load was high and the traffic went up. It was strange that I did not find the cause for two days. Later I saw one article, this article describes how to solve the 502 error in nginx. We tried this method and finally found the cause of the problem.

The solution is as follows:

1. Check whether the current PHP FastCGI process count is sufficient.

Netstat-anpo | grep "php-cgi" | wc-l

If the actual "FastCGI process count" is close to the preset "FastCGI process count", it indicates that "FastCGI process count" is insufficient and needs to be increased.

2. If the execution time of some PHP programs exceeds the Nginx waiting time, you can add the FastCGI timeout time in the nginx. conf configuration file, for example:

In the first step, the current number of PHP FastCGI processes in the system significantly exceeds the default value of 64, check that the current PHP FastCGI process count on the Telecom server is not higher than 64, and the active connection of the Netcom line is significantly higher than the active connection of the Telecom. Check the situation at night, as a result, at, the current number of PHP FastCGI processes in the system is significantly smaller than the 64 preset value, and the current active connection is much lower than the original number, this shows that nginx is unstable because the server access load is too high, that is, the second step of the error does not work.

To sum up, if the php-cgi process is insufficient, the php Execution time is long, or the php-cgi process is dead, a 502 error will occur.

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.