What do you mean 502 bad gateway? 502 Fault Gateway Error Resolution _ Server Other

Source: Internet
Author: User
Tags fpm php script browser cache

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

Successfully resolved 502 Bad Gateway error

Log on Today, show 502 bad Gateway,nginx the most annoying place is often this error, because I do not intend to spend too much time on the technology, so the general is encountered related problems, through the search engine solution can not go into the deep. Don't say much nonsense, talk about my bumpy experience.
First, I restarted the server, but restarted several times, are invalid, and then went to Google search the next 502 Bad Gateway, out of a lot of web pages, filtered into the http://lnmp.org/faq/ Lnmp-nginx-502-bad-gateway.html, the author summed up very detailed, basically do is, the problem is, I am completely not right, because the path listed inside or PHP-FPM and php-cgi does not exist, this baffled me. So it occurred to me that instead of blindly looking for reasons, I could restart the Web server. The results came out.

After Google, a friend gives

Httpd:syntax error on line Of/usr/local/apache/conf/httpd.conf:module Rewrite_module are built-in and can ' t be loaded

Indicates that the module is built inside, no need to be transferred, comment out

#LoadModule Rewrite_module modules/mod_rewrite.so

The following are supplementary:
then, according to the comments, restart the Web server, sure enough, to and fro, and back and forth, made a lot of time, mainly a thinking question. Find out where the problem is, and then find a way to solve the problem. This is mainly used for recording, save for later. cause

A server (not necessarily a Web server) is a gateway or proxy to meet customer requirements, such as a Web browser or our Checkupdown robot, to access the requested URL. This server received an invalid response from an upstream server to fulfill its requirements.
Fixed 502 Error
Generally this problem is due to poor IP communication between the backend computers, including the websites you may try to access on the Web server. In analyzing this problem, you should clear the browser cache completely.
If you look at this problem on all the sites you try to access on the Internet, 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 does not function properly.
In the first case, only your ISP can help you. In the second case, what you need to solve is to stop you from getting into the Internet.
If you only have this problem in a part of the Web site you're trying to access, it's probably a problem--one of those sites that has a device failure or overload. Contact the administrator of the Web site.

about 502 Bad Getway the solution to the error of the popular explanation

1. What is 502 bad Getway error
In short, 502 is an error type code bad Getway wrong gateway
2. Causes of Errors
Connection Timeout We send a request to the server because the server is currently linked too much, causing the server to not give the normal response, resulting in such an error
3. Means of salvation
The best solution, of course, is to do it on the server, which is unlikely for everyone.
So what can we do to save it?
Frankly, it's simple.
IS--refresh (not a general refresh OH)
The principle of refreshing: Many people may not know that there are two types of refreshes.
The so-called refresh is actually downloading data from the server to the local hard disk browser,
Then read the data from the local hard disk to the browser to show us.
① Basic Refresh: Click Refresh or use F5 shortcut keys
Basic refresh simply picks up the data from the local hard disk to the browser and does not request the server again.
Most users are often so refreshed, encountered 502 of the error will have no effect.
② from the server refresh: If you re-click on the page you want to browse the link, you will find that the page is still showing 502 bad Getway now can be normal browsing!
Do you understand the truth? When you click on the page you want to browse the link, it will be downloaded from the server data.
The workaround is to refresh from the server: shortcut key Ctrl+f5, which is to resend the request to the server.
If the server can give you a normal response you can see the page.


in-depth analysis of Nginx 502 Bad Gateway and Nginx 504 Gateway time-out and its solution

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 around the same time online.
But the people on the line need to request php-cgi to do a lot of information processing, so I set the 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 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.


Nginx 502 Bad Gateway Error resolution method


When using Nginx as a Web server, you are more or less likely to encounter errors in Nginx 502 bad Gateway, causing many of these errors. Here we come to one by one parsing.

First, see if the php-cgi is running
Sometimes because the site traffic is too large or other reasons, resulting in php-cgi directly down, so we have to see if php-cgi is running. Execute the following command:

ps-a | grep php5-cgi
If it is not running, start manually

/etc/init.d/php_cgi start
If you find php-cgi unexplained and sometimes down, you can use the following script to temporarily resolve 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, the number of fastcgi processes, PHP execution time for a long reason
The number of fastcgi processes can modify the value of the Max_children in php-fpm.conf, the maximum memory consumed at peak time is 20M, calculated according to your memory.
Restricting PHP execution time can be set in the php-fpm.conf request_terminate_timeout to prevent a bug in the PHP program from causing php-cgi to feign death.

Third, fastcgi execution time is too long
Increase the following parameter values according to the actual situation

Fastcgi_connect_timeout 300; Fastcgi_send_timeout 300; Fastcgi_read_timeout 300;
In addition to the three cases listed above, there are certainly other reasons, but above three are the most common

In recent days found that the network of network traffic instability of the server, the specific performance is that the flow is sometimes high, sometimes low, when the flow of low time to find the system's load is very small, almost 0, but after a while, load and high, traffic also went up, is very strange, looking for 2 days did not find the reason, then see side of the article, This paper introduces the problem of solving the error of 502 in Nginx, and tries it according to this method, and finally finds the cause of it.

The resolution steps are as follows:

1. See if the current PHP fastcgi process count is sufficient

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

If the actual number of "fastcgi processes" is close to the preset "fastcgi process", then the "fastcgi process count" is not sufficient and needs to be increased.

2, some of the PHP program execution time than the Nginx wait time, you can appropriately increase the nginx.conf configuration file fastcgi timeout time, such as:

In doing the first step, the system is currently the number of PHP fastcgi process is significantly higher than the default value of 64, the number of servers on the telecommunications server to view the current PHP fastcgi no more than 64 of this value, and the network link is significantly more active connections than telecommunications, Ready to look at the time of the evening, the results of the night 22:30, to view the system the current number of PHP fastcgi process is significantly less than 64 preset, the current active connection is much lower than the original, which can be explained that the nginx instability is due to the server access load caused by too much, Just add a second step of the error also does not top the role.

Summary, php-cgi process number is not enough, PHP execution time is long, or php-cgi process dies, there will be 502 errors

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.