title: Docker nginx redirect HTTP to HTTPS
body: <p>I'm setting up a server using Docker. One container runs an nginx image with SSL configured. A second container runs with a simple node app (on port 3001). I've got the two containers communicating with a --link docker parameter.</p>
  
  <p>I need to redirect all HTTP requests to HTTPS. Looking at other threads and online sources, I found <code>return 301 https://$host$request_uri</code>. When I type <a href="http://localhost" rel="nofollow">http://localhost</a> in the browser I'm getting the upstream's name in the browser (<a href="https://node_app" rel="nofollow">https://node_app</a> instead of <a href="https://localhost" rel="nofollow">https://localhost</a>). How can I successfully redirect without defining a server_name or explicitly defining a domain?</p>
  
  <p><strong>Edit</strong>: I should clarify that accessing <a href="https://localhost" rel="nofollow">https://localhost</a> directly  in the browser works. HTTP does not.</p>
  
  <p>Here's my nginx.conf file:</p>
  
  <pre><code>worker_processes 1;
  
  events { worker_connections 1024; }
  
  http {
  
    upstream node_app {
      server node:3001;
    }
  
    server {
      listen 80;
      return 301 https://$host$request_uri;
    }
  
    server {
      listen 443 ssl;
  
      ssl_certificate /etc/nginx/ssl/server.crt;
      ssl_certificate_key /etc/nginx/ssl/server.key;
  
      location / {
        proxy_pass http://node_app/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
      }
  
    }
  
  }
  </code></pre>