What does 502 bad gateway mean 502 bad gateway error resolution

  • 2020-05-09 19:38:20
  • OfStack

You can try clearing the browser cache to access 1 of your FTP to see if you can log in

The 502 Bad Gateway error was successfully resolved

Today, I logged into the blog and found 502 bad gateway. The most annoying thing about NGINX is that it often makes this mistake. Since I don't intend to spend too much time on technology, I usually encounter some relevant problems, which can be solved through the search engine, so I won't go into the details. Without further ado, tell me about my ups and downs.
First of all, I restart the server, restart several times, however, are invalid, then went Google search under 502 bad gateway, come out a lot of web page, filtering into http: / / lnmp org faq/lnmp - Nginx - 502 - Bad - Gateway. html, the author summarized is very detailed, basically according to just do it, the problem is that I completely right, Because the paths listed or php-fpm and php-cgi don't exist, this beats me. So it occurred to me that instead of blindly looking for a reason, I should restart the web server again. So here we go

Google will be given by a friend

httpd: Syntax error on line 54 of /usr/local/apache/conf/httpd.conf: module rewrite_module is built-in and can't be loaded

Means the module is built in, no need to call in, comment out

#LoadModule rewrite_module modules/mod_rewrite.so

The following is a supplement:
Then, according to the comments out, restart the web server, and sure enough OK, back and forth, after a lot of time, the main problem is a train of thought. Figure out where the problem is and then figure out how to solve it. This is mainly used for record keeping, reserved for future use. The reasons causing

The server (not necessarily the Web server) ACTS as a gateway or proxy to access the requested URL at the request of the customer (such as the Web browser or our CheckUpDown robot). This server received an invalid response from an upstream server to fulfill its request.
Fixed 502 error
1 this problem is due to poor communication between IP back-end computers, including websites you may try to visit on Web servers. Before analyzing this issue, you should clear the browser cache completely.
If you look at this question on all the websites you try to visit when you go online, there are two possibilities
1) your ISP has a major equipment failure/overload or
2) there is a problem with the internal Internet connection, such as your firewall cannot function normally.
In case 1, only your ISP can help you. In the second case, what you need to fix is to block your access to the Internet.
If you have this problem only in some of the sites you are trying to visit, it is likely to be a problem, that is, one of those sites, with equipment failure or overload. Contact the webmaster.

 's popular explanation of the solution to the error reporting of 502 bad getway

  1. What is 502 bad getway reporting an error  
Simply put,   502 is the gateway   for the error type code bad getway
  2. Reason for error    
  connection timeout we send a request to the server because the server is currently too linked, so the server cannot give a normal response, resulting in this kind of error report  
3.  
The best solution for  , of course, is to do it on the server
So what are our remedies?  
  makes it very simple.  
  is -- refresh (not refresh like 1)  
  refresh principle: many people may not know that there are two types of refresh.  
 , which downloads data from the server to the local hard disk browser,  , is a refresh
  then reads the data from the local hard disk and displays it to us in the browser.  
  basic refresh: click refresh or use F5 shortcut  
The basic refresh of   simply retakes the data from the local hard disk to the browser and does not reissue the request to the server.  
Most users of   refresh in this way most of the time, and there is no effect when they encounter an error of 502.  
If you click directly on the page you want to view again, you will find that the page that displayed 502 bad getway is now ready to view again!  
Does   get the point? When you click on a link to the page you want to visit, the data will be downloaded from the server again.  
The   solution is to refresh from the server: the shortcut ctrl+F5 is used to re-send the request to the server.  
  you can see the page if the server responds to you normally.  


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

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process has been terminated for some reason (1 is usually a reading resource problem).
Nginx 504 Gateway Time out means that the requested gateway has not been requested, in short, PHP-CGI has not been requested.
Generally speaking, Nginx 502 Bad Gateway and php-fpm. conf are related to the Settings of nginx. out, while Nginx 504 Gateway Time-out are related to the Settings of nginx. conf.
The correct setup takes into account multiple factors such as the performance of the server itself and the number of visitors.
Take my current server as an example, CPU is about 41.5G, 1GB memory, CENTOS system, visitors are about 50 people online at the same time.
But most people online need to request PHP-CGI for a lot of information processing, so I set nginx.conf as:
300 s fastcgi_connect_timeout;
300 s fastcgi_send_timeout;
300 s fastcgi_read_timeout;
128 k fastcgi_buffer_size;
128 k fastcgi_buffers 8; # 8, 128
256 k fastcgi_busy_buffers_size;
256 k fastcgi_temp_file_write_size;
fastcgi_intercept_errors on;
The main Settings here are the first three, namely
300 s fastcgi_connect_timeout;
300 s fastcgi_send_timeout;
300 s fastcgi_read_timeout;
The time required to connect, send, and read for PHP-CGI is specified here, and 300 seconds is sufficient, so my server rarely has the error of 504 Gateway Time-out. The most important is the setting of php-fpm.conf, which will directly lead to 502 Bad Gateway and 504 Gateway Time-out.
Let's take a closer look at the key parameters of php-fpm.conf:
php-fpm.conf has two crucial parameters, one is "max_children", the other is "request_terminate_timeout".
One of my two Settings is "40 "and one is "900", but this value is not generic, it needs to be calculated by myself.
The calculation method of   is as follows:
If your server is performing well and your broadband resources are sufficient, you can set "request_terminate_timeout" to 0s if the PHP script does not have a loop or BUG. The meaning of 0s is to allow PHP-CGI1 to proceed without a time limit. If you can't do that, which means your PHP-CGI might have some BUG, or you don't have enough bandwidth or something that causes your PHP-CGI to fake death, then it's recommended that you assign a value to "request_terminate_timeout, "which can be set based on the performance of your server. Generally speaking, the better the performance is, the higher you can set it, from 20 minutes to 30 minutes. Since my server PHP script takes a long time to run, some of it may take more than 10 minutes, so I set 900 seconds so that PHP-CGI does not die and the error of 502 Bad gateway occurs.
And how do you figure out "max_children"? In principle, the larger the value, the better, and the more processes php-cgi have, the faster they will be processed and the fewer requests queued. The setting of "max_children" also needs to be set according to the performance of the server. Generally speaking, the memory consumed by each php_cgi is about 20M for every php_cgi for 1 server normally, so I set my "max_children" to 40, and 20M*40=800M, which means that the memory consumed by all PHP-CGI is within 800M at the peak, which is lower than my effective memory 1Gb. If my "max_children" is set to a smaller number, say 5-10, then php-cgi will be "tired", slow to process, and have a longer wait time. If the request has not been processed for a long time, the error of 504 Gateway Time-out will appear, and the error of 502 Bad gateway will appear if the several php-cgi that are being processed are very tired.


Nginx 502 bad gateway error resolution


When using Nginx as Web server, you will more or less encounter the error of Nginx 502 bad gateway. There are many reasons for this error. So let's do 11.  
 

1. Check to see if php-cgi is running  
Sometimes, due to excessive website traffic or other reasons, php-cgi is directly dropped from down, so we have to see if php-cgi is running. Execute the following command:

ps -A | grep php5-cgi  
If not, start manually

/etc/init.d/php_cgi start  
If you find that php-cgi is sometimes missing for unknown reasons, you can use the following script to temporarily fix the problem and add it to cronjob.

if ps aux | grep 'php5-cgi' | grep-v grep   > / dev/null; then             echo "PHP-cgi is runnning!" "PHP-cgi down Starting over..."                  /etc/init d/php - fcgi start   fi 2. fastcgi processes   php execution time not enough use, long reasons
The number of fastcgi processes can modify the max_children value in php-fpm. conf. The maximum memory consumption of php-cgi at peak time is 20M.
The limitation of php execution time can be set in php-fpm.conf for request_terminate_timeout to prevent php program bug from causing php-cgi to suspend its execution.

3. The execution time of FastCGI is too long
Adjust the value of the following parameters according to the actual situation

fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;  
There are, of course, other causes besides the three listed above, but the three above are the most common

Recently found that the flow instability CNC server line in a few days, the specific performance is that the high flow from time to time, and sometimes low, found in low flow when the load of the system is very small, almost to zero, but 1 will, load and high up, traffic, also is very strange, looking for the 2 days didn't find the reason, then see 1 edge article, introduces the solve nginx appear 502 error phenomenon, according to this method to try, finally found the cause of the problem.

The solution steps are as follows:

1. Check to see if the current PHP FastCGI processes are sufficient

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

If the actual number of "FastCGI processes "used is close to the preset number of "FastCGI processes", then the number of "FastCGI processes "is insufficient and needs to be increased.

2. The execution time of some PHP programs exceeds the waiting time of Nginx, so the timeout time of FastCGI in nginx.conf configuration file can be appropriately increased, for example:

When doing the step 1, the system of current PHP FastCGI process for this value is significantly more than the default value of 64, in the telecommunications server to view the current PHP FastCGI process for this value is not higher than 64, and net line activities activities significantly higher than the telecom connections, ready to into the evening look at the situation, the results into the evening 22: At the time of 30, the current number of PHP FastCGI processes in the system is obviously less than the preset value of 64, and the current active connection is much lower than the original value. Therefore, it can be seen that the unstable situation of nginx is caused by the excessive server access load, even if the error in step 2 is added, it will not work.

In summary, a 502 error occurs when the php-cgi process runs out of time, php takes a long time to execute, or the php-cgi process dies


Related articles: