Technology Sharing

Analysis of the principle and solution of 502 Bad Gateway in browser

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Preface

Such problems are mainly difficult to solve

1. As shown in the question

insert image description here

2. Principle Analysis

502 The Bad Gateway error indicates that the server received an invalid response from an upstream server when acting as a gateway or proxy.

Typically found on networks acting as proxies or gatewaysserverOn a server such as Nginx, Apache, or a load balancer

Generally, this situation mainly focuses on the following issues:

  • The upstream server is down: The upstream server may be down for a variety of reasons, including hardware failure, software crash, or network problems

  • Server Overload:The upstream server may be overloaded due to processing too many requests and cannot respond in time

  • Internet problem: There may be problems with the network connection between servers, such as timeout, packet loss, etc., which makes it impossible to establish a stable connection

  • DNS Issues: DNS resolution errors may cause the server to be unable to find the correct IP address of the upstream server

3. Solution

  1. First check whether you can ping the corresponding IP address

  2. Check DNS, or refresh DNS resolution cache (ipconfig /flushdns

insert image description here

The above can be easily checked by customer personnel

If you are a developer, you need

  1. Checking the upstream server status: Make sure the upstream server is running properly and able to handle requests. You can check the status of the upstream server by directly accessing it.

  2. Check Server Load: Make sure the upstream server is not overloaded. If the load is too high, consider increasing server resources or optimizing application performance

The general logic is as follows:

  • Check Nginx error log: Check the error log on the Nginx server, usually located at /var/log/nginx/error.log, to determine the specific cause of the error
tail -f /var/log/nginx/error.log
  • 1
  • Checking the upstream server status: Access the upstream server directly through curl or browser to check its response status
curl -I http://upstream_server_address
  • 1
  • Check Nginx configuration: Make sure Nginx is configured correctly, especially the address of the proxy_pass directive
location / {
    proxy_pass http://upstream_server_address;
}
  • 1
  • 2
  • 3
  • Check network connection: Use ping or traceroute to check the network connection between Nginx and the upstream server
ping upstream_server_address
traceroute upstream_server_address
  • 1
  • 2

In actual combat, in some cases, you can refresh the DNS resolution

For the server, you can restartNginx Configuration

Generally, there are too many requests, which makes it impossible to parse individual requests.