title: Nginx reverse proxy : Redirect all http requests to https
body: <p>I have a reverse proxy with Nginx running on port 5000 and I want to redirect all the requests coming to port 5000 as a https request.</p>
  
  <p>Right now I am getting the error : 400 Bad Request The plain HTTP request was sent to HTTPS port</p>
  
  <pre><code>server {
             listen 5000 ssl;
             server_name myserver.com;
  
             location / {
                 proxy_pass                 http://127.0.0.1:8080;
                 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_set_header           X-Forwarded-Server  $host;
                 proxy_set_header           X-Forwarded-Host  $host;
                 proxy_set_header           Host  $host:5000;
  
  
                 add_header 'Access-Control-Allow-Methods' 'GET, POST';
                 add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
                 add_header 'Access-Control-Allow-Credentials' 'true';
  
                 # here comes the basic auth, after the options part
                 auth_basic            'Restricted';
                 auth_basic_user_file  path/to/.htpasswd;
  
             }
  
             ssl    on;
             ssl_certificate    path/to/crt;
             ssl_certificate_key    path/to/key;
         }
  </code></pre>
  
  <p>Well I tried with adding</p>
  
  <pre><code> if ($scheme != "https") {
      rewrite ^ https://$host$request_uri permanent;
   }
  
   if ($scheme != "https") {
       return 301 https://$host$request_uri permanent;
   }
  </code></pre>
  
  <p>Nothing seems to solve the issue. What should I do to fix this?</p>