title: Nginx HTTP not redirecting to HTTPS 400 Bad Request "The plain HTTP request was sent to HTTPS port"
body: <p>I'm running nginx in docker. HTTPS works fine but when I explicitly make HTTP request I get the following error</p>
  
  <p>400 Bad Request
  The plain HTTP request was sent to HTTPS port</p>
  
  <p>nginx.conf is as follows</p>
  
  <pre><code>worker_processes auto ;          
  events {}
  
  http {
  
  include /etc/nginx/mime.types;
  
  access_log /var/log/nginx/main.access.log;                                           
  
  server {    
  listen 80;                                                                                                       
  location / {
      return 301 https://localhost:3000$request_uri; 
  }
  
  }
  
  server {   
  listen 443 ssl;                                                      
  server_name  localhost:3000;                  
   root    /var/www/html; 
  
  ssl_certificate         /etc/nginx/ssl/cert.pem; 
  ssl_certificate_key     /etc/nginx/ssl/key.pem;
  
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
  
  location / {
  try_files  $uri /index.html;        
  }
  
  }
  
  }
  </code></pre>
  
  <p>I run this container using </p>
  
  <pre><code>docker run -p 3000:443 -it -d --name nginxtest nginx-test
  </code></pre>
  
  <p>and get the following error</p>
  
  <p>docker file is as follows</p>
  
  <pre><code>FROM nginx:latest
  COPY ./build /var/www/html
  COPY ./nginx.conf /etc/nginx/nginx.conf
  COPY ./ssl /etc/nginx/ssl
  EXPOSE 443
  CMD [ "nginx","-g","daemon off;" ]
  </code></pre>
  
  <p>Weird thing is that sometimes it works perfectly fine, and all of a sudden it stops working and won't even work if I recreate the containers.</p>
  
  <p>Even tried doing the following. Still no luck</p>
  
  <pre><code> server {    
      listen 80;                                                                                                       
       server_name localhost:3000
          return 301 https://localhost:3000$request_uri; 
      }
  </code></pre>
  
  <p>Another odd thing when I run the following docker command</p>
  
  <pre><code>docker run -p 3000:443 -p 3001:80 -it -d --name nginxtest nginx-test
  </code></pre>
  
  <p>and go to localhost:3001 it redirects me to https just fine but other things do break. 
  Sorry for the long question</p>