Solution: 502 Bad Gateway

Source: Internet
Author: User
Tags domain name server install wordpress nginx reverse proxy
Solution: 502 Bad Gateway

Http://my.oschina.net/junn/blog/147923?fromerr=OTLUXxE6

502 Bad Gateway server as a gateway or proxy, the server returned an illegal reply in order to complete the request to access the next server.

The solution is: Refresh the Web page or clean up the computer's buffer file when you open the page you want to open it. (Generally, this approach is OK, but also does not rule out the Web page you visit is blocked, if you visit the page is blocked, no matter how you refresh is useless)

1. What is 502 Bad Gateway error

In short, 502 is an error type code bad Gateway

2. Causes of 502 errors

Connection Timeout We send a request to the server because the server is currently too many links, resulting in the server can not give a normal response, resulting in such an error, as follows:

The first reason:

DNS Buffering. The usual reason for this is because you have access to a website like Facebook without a VPN turned on.

This time the natural access is not at the same time, but in this machine left a buffer.

This situation is usually accessible within a few minutes. You can also try to run Ipconfig/flushdns in a DOS window, which refreshes the DNS buffer.

A second reason:

Your browser has an agent or something. Make sure to turn off the agent.

A third reason:

DNS is hijacked, even if you use foreign DNS, it will be hijacked. Some of the machines can access the VPN, some of the machine does not. And the reason for proxy, firewall, local network is excluded. Ping the remote site at the same time, such as Facebook. The unreachable machine usually gets a weird IP that pings from anywhere. and can access the IP of the machine, can not access the machine directly accessible, you can also ping through. In this case we can remove the DNS of the VPN server.

Switch to additional DNS. In the Windows system, you can remove the default DNS from the properties of the local network connection, using foreign DNS, such as Google's. or OpenDNS.

3.502 Wrong HTTP Period

Any client, such as a Web browser or our Checkupdown robot, communicates with your Web server through the following loops:

Get the IP name of your website's IP address (the leader of your website URL ' http://'). This looks for the domain name server (DNSS) that is provided by the name of the converted IP (IP address).

Open an IP socket to connect to the IP address. Write an HTTP data stream through the socket.

You receive an HTTP data stream from the Web server in which you responded. This data stream contains the value of the status code that is represented by the HTTP protocol. Parse the status code and other useful information for this data stream.

When this error occurs at the last step, the client above receives an HTTP status code that confirms 502 '.

4. Fixed 502 Error

Generally this problem is due to poor IP communication between the backend computers, including websites that you may be trying to access on the Web server. In analyzing this problem, you should completely clear the browser cache.

If you look at this problem on all the websites you are trying to access online, there are two possible

1) Your ISP has a major equipment failure/overload

2) Problematic internal Internet connection such as your firewall is not functioning properly.

In the first case, only your ISP can help you. In the second case, you will need to resolve any issues that prevent you from entering the Internet.

If you only have this problem in some of the websites that you are trying to access, it is likely that one of these sites has a malfunctioning or overloaded device. Contact the administrator of the site.

5. How 502 bad Gateway solves the problem

The simplest way: Ctrl+f5 forced to refresh the best solution of course or on the server is not very likely for everyone, then we have what to save the way? In plain simple, that is--refresh (not a normal refresh OH)

The principle of refresh: Many people may not know that there are two kinds of refreshes. The so-called refresh is actually from the server to download data to the local hard drive browser, and then from the local hard disk to read data to the browser display to us.

① Basic refresh: Just click to refresh or use the F5 shortcut key, basic refresh just from the local hard drive to fetch data to the browser, do not re-make a request to the server. Most of the users are so refreshed, encountered 502 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 link of the page you want to browse, it will download the data from the server again. The workaround is to refresh from the server: shortcut key Ctrl+f5, which is to re-send the request to the server. If the server responds properly to you, you can see the page.

Also attached:

The cause and solution of Nginx 502 Bad Gateway error

Nginx 502 Bad Gateway error has been encountered several times, here to do a record, memo haha.

There are a lot of situations where there are 502 errors, let's take a look at the situation below.

One, fastcgi buffer set too small

Error, first to find Nginx log file, directory is/var/log/nginx, in the log found the following error.

2013/01/17 13:33:47 [ERROR] 15421#0: *16 upstream sent too big header while reading response headers from upstream

Look up the information, to the effect that the nginx buffer has a bug caused, our site page consumption may be too large buffer.

Online Find a solution, in the foreign web site saw an increase buffer method, completely solve the Nginx 502 bad gateway problem. Here's how:

HTTP {    ...    Fastcgi_buffers 8 16k;    Fastcgi_buffer_size 32k;    ...}

Please increase the above two configuration items yourself according to the server's website.

Two, proxy buffer set too small

If you are using the Nginx reverse proxy, if the header is too large, beyond the default of 1k, it will trigger the above upstream sent too big header (it is said nginx to the external request to the backend processing, the back end of the header is too large, Nginx processing will lead to 502.

server {        listen       ;        server_name  *.lxy.me;         Location/{############## #添加这3行            proxy_buffer_size 64k;            Proxy_buffers   32k;            Proxy_busy_buffers_size 128k;############## #添加这3行            proxy_set_header Host $host;            Proxy_set_header x-real-ip       $remote _addr;            Proxy_set_header x-forwarded-for  $proxy _add_x_forwarded_for, ......}

Third, the default php-cgi process number is set too little

502 problems occur during the installation, generally because the default php-cgi process is 5, possibly because the phpcgi process is not enough to cause 502, need to modify/usr/local/php/etc/php-fpm.conf Max_ The children value is increased appropriately. It is also possible that the max_requests value is not sufficient. It is important to note that this configuration is very memory-intensive, and should be set according to the server configuration. Otherwise, it may play a counter-effect.

Iv. PHP Execution Timeout

PHP execution timed out, modify/usr/local/php/etc/php.ini to change Max_execution_time to 300

Five, nginx wait time timeout

The execution time of some PHP program exceeds nginx wait time, can increase the timeout time of fastcgi in nginx.conf config file appropriately.

HTTP  {  fastcgi_connect_timeout;  Fastcgi_send_timeout;  Fastcgi_read_timeout;  ......  }

Nginx 502 Bad Gateway

Some websites that run on Nginx sometimes appear "502 Bad Gateway" errors, even when they occur frequently. The following is a small compilation of some of the Nginx 502 error troubleshooting methods, for reference:

The reason for Nginx 502 error is more because of a problem with the backend server in proxy mode. These errors are generally not the problem of nginx itself, be sure to find the reason from the back end! But Nginx put these mistakes are in their own body, really let Nginx promoter is questioned, after all, from the word understanding, bad gateway? Isn't that bad nginx? Let not understand the people see, will directly put the blame on the Nginx body, I hope nginx next version will be the wrong hint to write a little more friendly, at least not now a simple sentence 502 bad Gateway, also do not forget to attach their own name.

Trigger conditions for Nginx 502

The most common occurrence of a 502 error is the backend host machine. In the upstream configuration, there is a configuration: Proxy_next_upstream, this configuration specifies the Nginx in the data from a backend host to get the error when it will go to the next back-end host, which is written in the 502 will appear all the situation pull, default is error Timeout Error is when the machine, disconnection, and so on, timeout is to read the blocking timeout, more easily understood. I usually write it all up:

Proxy_next_upstream Error timeout invalid_header http_500 http_503;

But now maybe I want to get rid of the http_500, http_500 specifies that the backend returns a 500 error when it turns a host, the back end of the JSP error, it would have printed a bunch of stacktrace error messages, is now replaced by 502. But the company's programmers do not think so, they believe that the nginx error, I really do not have time to explain to them the principle of 502 ...

503 error can be preserved, because the backend is usually Apache resin, if the Apache panic is error, but resin panic, just 503, so it is necessary to keep.

Solutions

If you encounter 502 problems, you can prioritize the following two steps to resolve them.

1. See if the current number of PHP fastcgi processes 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, then the number of "fastcgi processes" is not sufficient and needs to be increased.

2, some PHP program execution time exceeds the Nginx wait time, can appropriately increase the timeout time in the nginx.conf configuration file fastcgi, for example:

HTTP  {  fastcgi_connect_timeout;  Fastcgi_send_timeout;  Fastcgi_read_timeout;  ......  }  ......

PHP.ini in Memory_limit set low will error, modified php.ini memory_limit for 64M, restart Nginx, found good, the original is the memory of PHP is insufficient.

If this modification does not solve the problem, you can refer to the following scenarios:

First, Max-children and max-requests

An nginx php (FPM) XCache is running on a single server with a daily average of 300W of PV traffic.

Recently, this is often the case: PHP page Open very slow, the CPU usage suddenly dropped to very low, the system load suddenly up to very high, look at the network card traffic, will also find suddenly dropped to very low. This only lasts a few seconds and then resumes.

Check the log files for php-fpm to find some clues.

Sep 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 08:32:23.296426 [NOTICE] Fpm_init (), Lin E 47:fpm is running, PID 30587

In front of these sentences, there are more than 1000 lines of closed children and open children logs.

Originally, PHP-FPM has a parameter max_requests, which indicates how many requests per children will be closed and the default setting is 500. Because PHP is polling the request to each children, it takes about the same amount of time for each childre to reach max_requests at large traffic, which causes all children to be shut down essentially at the same moment.

During this time, Nginx can not transfer php files to PHP-FPM processing, so the CPU will be reduced to very low (not to process PHP, not to execute SQL), and the load will rise to very high (close and open children, nginx wait for php-fpm), network card traffic also dropped to very low ( Nginx cannot generate data transfer to client)

Solving the problem is simple, increase the number of children, and set the max_requests to 0 or a larger value:

Open/usr/local/php/etc/php-fpm.conf The following two parameters (according to the actual situation of the server, too large also not)

 
  
   
  5120
 
    
 
  
 
  

Then restart PHP-FPM.

Second, increase the buffer capacity size

Open the Nginx error log and discover "Pstream sent too big header while reading response headers from upstream" error message. Look up the information, to the effect that the nginx buffer has a bug caused, our site page consumption may be too large buffer. Refer to the foreigner write modification method increases the buffer capacity size setting, 502 problem solved completely. Later, the system administrator also adjusted the parameters to retain only 2 settings parameters: Client head buffer,fastcgi buffer size.

Third, Request_terminate_timeout

If this is the case when there are several post or database operations, but not common in static page operations, you can look at one of the php-fpm.conf settings:

Request_terminate_timeout

This value is Max_execution_time, which is the fast-cgi execution script time.

0s

0s is closed, is infinite execution goes on. (When the outfit was not carefully looked at the change of a number) problem solved, the implementation of a long time will not be wrong. Optimization fastcgi, you can also change the value of 5s to see the effect.

A 502 error occurs when the number of php-cgi processes is insufficient, the PHP execution time is long, or the php-cgi process dies.

    1. WordPress background page appears 502 Bad Gateway Error resolution method
    2. Lnmp install wordpress 3.8.1 appear 502 Bad Gateway solution
  • 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.