pythonamazon-web-servicesflaskamazon-ec2

AWS ALB returns 502 Bad Gateway error against EC2 instance with Flask application


I'm trying to test Facebook OAuth so have an exact copy of some Flask OAuth login setup. I put it in my EC2 instance, bought a domain, and registered it on Route 53, made an application load balancer, and assigned my EC2 to it.

I'm using Flask's default port (5000), and my security group setting has inbound on port 5000 open, and all traffic on outbound open (like this)

The health check is working perfectly fine. I can see from Flask's logs that it's working and returning 200. But when I type in the domain name, it's returning 502 Bad Gateway.

On the EC2 instance side, where I've turned my app on, this returns code 400 Bad Request Syntax (like this). I tried changing encoding formats but this failed.

I've checked security ports, have checked my Route 53 A-type record sets, the EC2 instance itself seems to work fine, the health check from target groups goes all fine, and I'm out of ideas on where I've gone wrong.

EDIT: curl -vvv {domain_name} returns:

* Rebuilt URL to: www.*.xyz/
*   Trying 1****...
* TCP_NODELAY set
* Connected to www.*.xyz (*) port 80 (#0)
> GET / HTTP/1.1
> Host: www.*.xyz
> User-Agent: curl/7.53.1
> Accept: */*
>
< HTTP/1.1 502 Bad Gateway
< Server: awselb/2.0
< Date: Wed, 28 Mar 2018 10:43:58 GMT
< Content-Type: text/html
< Content-Length: 138
< Connection: keep-alive
<
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>
* Connection #0 to host www.*.xyz left intact

EDIT 2: The reason for my 502 Bad Gateway error was due to my target group was using HTTPS protocol. But I need this done with HTTPS since Facebook OAuth only works under HTTPS.


Solution

  • The 502 Bad Gateway error is an HTTP status code that means that ELB received an invalid response from the EC2 Instance.

    I guess you have selected HTTPS in your target group, create a new target group with HTTP and attach it to the load balancer.