Skip to content

HTTP Status Code 502 – Bad Gateway

HTTP Status Code 502 – Bad Gateway

The HTTP status code 502 Bad Gateway indicates that a server acting as a gateway or proxy received an invalid response from an inbound server it accessed while attempting to fulfill the request. This is often due to network issues, server overloads, or misconfigurations.

Example 1: Reverse Proxy Misconfiguration

Consider a scenario where you are using Nginx as a reverse proxy for your application server. If the proxy is misconfigured or if the application server is down, a 502 error may occur.


        server {
            listen 80;
            server_name example.com;

            location / {
                proxy_pass http://127.0.0.1:5000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
        }
        

In this configuration, if the application server at http://127.0.0.1:5000 is unreachable or not responding, the client will receive a 502 Bad Gateway error.

Example 2: Load Balancer Timeout

In a load balancing scenario, a 502 error might occur if the load balancer times out waiting for a response from an upstream server.


        {
            "LoadBalancerName": "my-load-balancer",
            "Listeners": [
                {
                    "Protocol": "HTTP",
                    "LoadBalancerPort": 80,
                    "InstanceProtocol": "HTTP",
                    "InstancePort": 80
                }
            ],
            "HealthCheck": {
                "Target": "HTTP:80/ping",
                "Interval": 30,
                "Timeout": 5,
                "UnhealthyThreshold": 2,
                "HealthyThreshold": 2
            }
        }
        

If the health check fails because the instance is slow or unresponsive, the load balancer will not route traffic to that instance, potentially resulting in a 502 error if no other healthy instances are available.

Example 3 Scenario

# Client sends a request example.
GET /example HTTP/1.1
Host: www.example.com

# Server Response
HTTP/1.1 502 502 Bad Gateway
Date: Wed, 09 Oct 2024 23:10:26 GMT
Server: ExampleServer/1.0
Content-Type: application/json

{
    "error": "Description of the error for 502"
}

Example 4 Scenario

# Client sends another example request.
POST /another-example HTTP/1.1
Host: www.example.com

# Server Response
HTTP/1.1 502 502 Bad Gateway
Date: Wed, 09 Oct 2024 23:10:26 GMT
Server: ExampleServer/1.0
Content-Type: application/json

{
    "error": "Detailed message for 502"
}

Summary

The 502 Bad Gateway error typically arises when there is an issue with the server acting as a gateway or proxy, or when the upstream server is malfunctioning. Common causes include server overload, network connectivity issues, misconfigurations, or application bugs. Solving a 502 error requires checking server configurations, ensuring upstream server availability, and monitoring network health.

I am the founder of SEO Leaders and have been involved in the internet and web development in one way or another for over 20 years. Since founding SEO Leaders some 6 years ago I have been heavily involved in web develepment, Digital PR and technical SEO for a wide variety of projects. I hope to enlighten you on a wide range of topics related to my chosen profession!

Back To Top