Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve 502 Bad Gateway errors with Elastic Load Balancer and EC2/Nginx for HTTPS requests?

I'm running into '502 Bad Gateway' issues for HTTPS requests when using AWS Elastic Load Balancer (Application type) in front of EC2 instances running Nginx. Nginx is acting as a reverse proxy on each instance for a waitress server serving up a python app (Pyramid framework). I'm trying to use TLS termination at the ELB so that the EC2 instances are only dealing with HTTP. Here's the rough setup:

Client HTTPS request > ELB (listening on 443, forwarding to 80 on backend) > Nginx listening on port 80 (on Ec2 instance) > forwarded to waitress/Pyramid (on same ec2 instance)

When I make requests on HTTPS I get the 502 error. However, when I make regular HTTP requests I get a response as expected (same setup as above except ELB is listening on port 80).

Some additional info: ELB health checks are working. All VPC/Security groups are configured correctly (I believe). I'm using an AWS certificate on the ELB using the standard setup/walkthrough on AWS. I SSH'd into the Ec2 instance and in the Nginx access log it looks like the HTTPS request are still encrypted? Or some encoding issue?

It looks like this

And here's nginx.conf on the EC2 instance:

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log /etc/nginx/access.log;  
    sendfile        on;

    # Configuration containing list of application servers
    upstream app_servers {

        server 127.0.0.1:6543;
    }   

    server {
        listen       80;
        server_name  [MY-EC2-SERVER-NAME];


        # Proxy connections to the application servers
        # app_servers
        location / {

            proxy_pass         http://app_servers;
            proxy_redirect     off;
            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-Host $server_name;

        }
    }
}
like image 839
Luke Avatar asked May 01 '18 15:05

Luke


People also ask

What causes 502 Bad gateway AWS?

HTTP 502 (bad gateway) errors can occur for one of the following reasons: The web server or associated backend application servers running on EC2 instances return a message that can't be parsed by your Classic Load Balancer.

What causes 502 Bad gateway nginx?

In more technical words, A 502 Bad Gateway means that the proxy (gateway) server wasn't able to get a valid or any response from the upstream server. If you are seeing a 502 bad gateway error on a website, it means that the origin server sent out an invalid response to another server that acted as a gateway or proxy.


1 Answers

Ok I figured it out (I'm a dummy). I had two listeners set up on the ELB, one for 80 and one for 443, which was correct. The listener for 80 was set up correctly to forward to backend (Nginx) port 80 over HTTP as expected. The 443 listener was INCORRECTLY configured to send to port 80 on the backend over HTTPS. I updated the 443 listener to use the same rule as the 80 listener (i.e. listen on 443 but send to backend 80 over HTTP) and it worked. Disregard y'all.

like image 169
Luke Avatar answered Sep 27 '22 18:09

Luke