Solution: 502 badgateway solution: 502 bad gateway
Http://my.oschina.net/junn/blog/147923? Fromerr = OTLUXxE6
502 when the Bad Gateway server acts as a Gateway or proxy, the server returns an invalid response to access the next Server to complete the request.
Solution: refresh the web page or clear the computer's buffer file and open the page you want to open. (In general, this method works, but it does not rule out the possibility that your webpage is blocked. if your webpage is blocked, it's useless no matter how you refresh it)
1. what is error 502 bad gateway?
In short, 502 is the gateway with the error type code bad gateway.
2. cause of 502 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. This type of error is reported as follows:
The first reason is:
DNS buffer. This is usually because you have accessed a website like facebook without enabling vpn.
At this time, access is not allowed, but the server is buffered.
This usually takes several minutes to access. You can also try to run ipconfig/flushdns in the dos window. this command will refresh the DNS buffer.
The second reason is:
Your browser opens proxy or something. Check whether the proxy is disabled.
Third reason:
Dns is hijacked, even if foreign dns is used. Some machines can access the vpn, but some machines cannot. The reason for proxy, firewall, and local network is also excluded. At this time, ping the remote website, such as facebook. A machine that cannot be accessed usually obtains a weird ip address, which cannot be pinged from any place. The sub-ip address that can be accessed can be directly accessed on the sub-host that cannot be accessed, or pinged. In this case, we can remove the DNS of the VPN server.
Switch to another dns. In windows, you can remove the default dns in the local network connection attribute and select a foreign dns, such as google. Or opendns.
HTTP cycle with 3.502 errors
Any client (such as a Web browser or our CheckUpDown robot) communicates with your Web server after the following cycles:
Obtain the IP address of your website IP address (your website URL leads to 'http ://'). This is the domain name server (DNSs) provided by (converted intellectual property name, IP address ).
Open an IP socket to connect to this IP address. Write an HTTP data stream through this socket.
Receives an HTTP data stream from your response Web server. The value of the status code contained in this data stream is determined by the HTTP protocol. Parse the status code and other useful information of this data stream.
When this error occurs in the last step, the client above receives an HTTP status code, which is confirmed to be 502 '.
4. 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 completely clear the browser cache.
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 experienced major device faults/overload
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, you need to resolve any problems that 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.
5. how to solve the 502 bad gateway problem
The simplest method: CTRL + F5 force refresh. The best solution is to do it on the server, which is unlikely for everyone. So what can we do to save it? To put it bluntly, it is very simple, that is, 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, and then read the data from the local hard disk to the browser display for us.
① Basic refresh: click to refresh or use the F5 shortcut key. The basic refresh is to retrieve data from the local hard disk to the browser again without sending 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.
Appendix:
Causes and Solutions for Nginx 502 Bad Gateway errors
The error of Nginx 502 Bad Gateway has been encountered several times. here we will record it and forget it.
The error 502 may occur in many situations. let's take a look at the situation below.
I. fastcgi buffer settings are too small
If an error occurs, first find the nginx log file in the/var/log/nginx Directory. the following error is found in the log.
13:33:47 [error] 15421 #0: * 16 upstream sent too big header while reading response header from upstream
After checking the information, the problem was caused by a bug in the nginx buffer zone. the page consumption on our website may be too large.
I found a solution on the Internet, and found a method for adding a buffer on a foreign website, which completely solved the problem of Nginx 502 Bad Gateway. The method is as follows:
http { ... fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; ...}
Increase the preceding two configuration items based on your website.
II. the proxy buffer settings are too small
If you are using nginx reverse proxy, if the header is too large, it exceeds the default 1 k, the above upstream sent too big header will be triggered (to put it bluntly, nginx will process external requests to the backend. if the header returned by the backend is too large, nginx will cause 502 if it cannot process it.
Server {listen 80; server_name *. lxy. me; location/{############# add the three rows proxy_buffer_size 64 k; proxy_buffers 32 32 k; proxy_busy_buffers_size 128 k; ############## add the three rows proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for ;............}3. the default number of php-cgi processes is too small.
502 problems occur during installation and use. Generally, the default php-cgi process is five, which may be caused by insufficient phpcgi processes, you need to modify/usr/local/php/etc/php-fpm.conf to add the max_children value as appropriate. It is also possible that the max_requests value is insufficient. It must be noted that this configuration item occupies a large amount of memory. please set it according to the server configuration. Otherwise, it may have a negative effect.
IV. php execution timeout
Php execution time-out. modify/usr/local/php/etc/php. ini and change max_execution_time to 300.
5. nginx wait time timeout
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 as appropriate.
http { fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; ...... }
Ngnix 502 bad gateway
Some websites running on Nginx sometimes encounter "502 Bad Gateway" errors, sometimes even frequently. The following are some troubleshooting methods for Nginx 502 errors collected by Xiaobian for your reference:
There are many causes of Nginx 502 errors because of problems with backend servers in proxy mode. These errors are not nginx problems, so you must find the cause from the backend! However, nginx has put all these errors on its own, which makes it highly questionable for nginx promoters. after all, we can understand the word "bad gateway? Isn't it bad nginx? People who do not know it will directly put the responsibility on nginx. I hope the next version of nginx will make the error prompt slightly more friendly, at least it's not a simple 502 Bad Gateway statement, and I don't forget to attach my name to it.
Nginx 502 trigger conditions
The most common occurrence of 502 errors is that the backend host is used as the machine. In the upstream configuration, there is a configuration: proxy_next_upstream, which specifies what errors nginx will encounter when retrieving data from a backend host to the next backend host, the default value is error timeout. An error occurs when a machine or disconnection occurs, and a timeout is a read congestion timeout, which is easy to understand. I generally write all of them:
proxy_next_upstream error timeout invalid_header http_500 http_503;
However, now I may want to remove the http_500 option. when http_500 specifies that the backend will return a 500 error, it will convert it to a host. if the backend jsp fails, a bunch of stacktrace error messages will be printed, it is now replaced by 502. But programmers in the company do not think so. they think that nginx has encountered an error. I really don't have time to explain the 502 principle to them ......
503 error can be retained, because the backend is usually apache resin. If apache crashes, it is error, but resin crashes, it is only 503, so it is necessary to keep it.
Solution
If you encounter a 502 problem, you can take the following two steps as a priority.
1. check whether the current PHP FastCGI Process count is sufficient:
netstat -anpo | grep "php-cgi" | wc -l
If the number of FastCGI processes actually used is close to the preset number of FastCGI processes, it indicates that the number of FastCGI processes 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:
http { fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; ...... } ......
An error occurs when memory_limit is set to low in php. ini. after modifying memory_limit of php. ini to 64 MB, restart nginx and check that PHP memory is insufficient.
If the problem persists, you can refer to the following solutions:
1. max-children and max-requests
Nginx php (fpm) xcache is running on one server, with an average traffic volume of around 300 PVS per day.
Recently, this situation often occurs: the php page is very slow to open, the cpu usage suddenly drops to a very low level, the system load suddenly rises to a very high level, view the network card traffic, you will also find that suddenly fell to a very low level. In this case, it takes only a few seconds to recover.
Check the log file of php-fpm and find some clues.
Sep 30 08:32:23.289973 [NOTICE] fpm_unix_init_main(), line 271: getrlimit(nofile): max:51200, cur:51200 Sep 30 08:32:23.290212 [NOTICE] fpm_sockets_init_main(), line 371: using inherited socket fd=10, “127.0.0.1:9000″ Sep 30 08:32:23.290342 [NOTICE] fpm_event_init_main(), line 109: libevent: using epoll Sep 30 08:32:23.296426 [NOTICE] fpm_init(), line 47: fpm is running, pid 30587
Before the preceding statements, the children and children logs are disabled for more than 1000 rows.
Originally, php-fpm has a max_requests parameter, which specifies the maximum number of requests processed by each children will be disabled. the default value is 500. Because php round-robin requests to every children, in the case of high traffic volumes, each Childe takes almost the same time to reach max_requests, which causes all children to be shut down at the same time.
During this period, nginx cannot forward the php file to php-fpm for processing, so the cpu will be reduced to a very low level (no php processing or SQL execution is required ), the load will rise to a very high level (turn off and enable children, nginx and wait for php-fpm), and Nic traffic will also decrease to a very low level (nginx cannot generate data to be transmitted to the client)
It is easy to solve the problem. increase the number of children and set max_requests to 0 or a relatively large value:
Turn on the/usr/local/php/etc/php-fpm.conf to increase the following two parameters (depending on the actual situation of the server, too big will not work)
5120
600
Restart php-fpm.
II. increase the buffer capacity
Open the error log of nginx and find an error message such as "pstream sent too big header while reading response header from upstream. After checking the information, the problem was caused by a bug in the nginx buffer zone. the page consumption on our website may be too large. According to the modification method written by foreigners, the buffer size setting is added, and the 502 problem is completely solved. Later, the system administrator adjusted the parameters and kept only two set parameters: client head buffer and fastcgi buffer size.
III. request_terminate_timeout
If it is common in some post or database operations, rather than static page operations, you can look at one of the php-fpm.conf settings:
Request_terminate_timeout
The value is max_execution_time, which is the script execution time of fast-cgi.
0 s
If 0 s is disabled, it is executed infinitely. (When I did not look at it carefully, I changed the number.) after the problem is solved, the execution will not go wrong for a long time. In fastcgi optimization, you can change the value to 5 s to see the effect.
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.
- Solution to 502 Bad Gateway error on the WordPress background page
- How to solve 502 bad gateway error in LNMP wordpress 3.8.1 installation