title: How to solve 502 Bad Gateway errors with Elastic Load Balancer and EC2/Nginx for HTTPS requests?
body: <p>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:</p>
  
  <p>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) </p>
  
  <p>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).</p>
  
  <p>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?</p>
  
  <p><a href="https://i.stack.imgur.com/2bSj7.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/2bSj7.jpg" alt="It looks like this"></a></p>
  
  <p>And here's nginx.conf on the EC2 instance: </p>
  
  <pre><code>#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;
  
          }
      }
  }
  </code></pre>