title: SSL proxy pass with NGINX leading to bad gateway through Docker
body: <h2>Background</h2>
  
  <p>My setup is based on the following tutorial:</p>
  
  <p><a href="https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/" rel="noreferrer">Dockerizing Django with Postgres, Gunicorn, and NGINX</a></p>
  
  TL;DR: (<em>italics: not covered by tutorial; personal adventure</em>)
  
  <ul>
  <li>3 Docker services for: nginx -> django -> postgres (arrow indicating dependency)</li>
  <li>Nginx proxy passes requests to exposed port in Django service.</li>
  <li>HTTP (non-SSL) requests working</li>
  <li><em>require SSL connections by redirecting http -> https</em></li>
  </ul>
  
  <hr>
  
  <h2>Details</h2>
  
  <p>I've generated a self-signed certificate to test out ssl redirects with NGINX locally before trying to get it to work on a VPS in production. I'm quite new to working with NGINX and so I'm not entirely sure what's going wrong or how to diagnose problems.</p>
  
  <p>Here's what I <strong>want</strong> to happen with the NGINX file I've provided below... <em>(spoilers: it doesn't)</em>:</p>
  
  <ol>
  <li>Go to <a href="http://localhost" rel="noreferrer">http://localhost</a></li>
  <li>Get redirected to <a href="https://localhost" rel="noreferrer">https://localhost</a></li>
  <li>Warning from browser about a self-signed cert; accept warning and proceed</li>
  <li>Site rendered fine, SSL redirect working!</li>
  </ol>
  
  <p>But this isn't the case. I get a 502 Bad Gateway, and NGINX outputs the following logs:</p>
  
  <pre><code>prod_1  | 192.168.144.1 - - [03/Jun/2019:00:01:44 +0000] "GET / HTTP/1.1" 502 158 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0" "-"
  prod_1  | 2019/06/03 00:01:44 [error] 8#8: *1 peer closed connection in SSL handshake while SSL handshaking to upstream, client: 192.168.144.1, server: , request: "GET / HTTP/1.1", upstream: "https://192.168.144.3:8000/", host: "localhost"
  </code></pre>
  
  <p>Can anyone tell me what's going on or how to fix it? I feel like there's probably a whole bunch wrong with my conf file even outside of the SSL redirect, but I don't really know how to identify any problems. The conf file is below...</p>
  
  <pre><code>upstream mysite {
      server web:8000;
  }
  
  # redirect http traffic to https
  server {
      listen 80;
      listen [::]:80;
  
      return 301 https://$host$request_uri;
  }
  
  server {
      listen 443 ssl;
      listen [::]:443 ssl;
  
      location / {
          proxy_pass https://mysite;
          proxy_ssl_server_name   on;
  
          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;
  
          proxy_redirect          off;
      }
  
      location /assets/ {
          alias /usr/src/site/assets/;
      }
  
      location /media/ {
          alias /usr/src/site/media/;
      }
  
      ssl_certificate         /etc/ssl/certificates/site.crt;
      ssl_certificate_key     /etc/ssl/certificates/site.key;
  
      ssl_session_cache           builtin:1000 shared:SSL:10m;
      ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers                 HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
      ssl_prefer_server_ciphers   on;
  
  }
  </code></pre>