1

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.

Right now I am getting the error : 400 Bad Request The plain HTTP request was sent to HTTPS port

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;
       }

Well I tried with adding

 if ($scheme != "https") {
    rewrite ^ https://$host$request_uri permanent;
 }

 if ($scheme != "https") {
     return 301 https://$host$request_uri permanent;
 }

Nothing seems to solve the issue. What should I do to fix this?

1

3 Answers 3

6

Assuming http traffic comes via port 80, you may redirect to https by adding an extra server block listening to this port:

server {
    listen 80;
    server_name myserver.com;

    location / {
        return 301 https://myserver.com$request_uri;
    }
}
0

This may sound simplistic, but have you tried adding "https://" to the front of the domain name you type in? I've found the default is always a plain "http" request.

4
  • Yes that works but after a while for some request the page refreshes and 'https://' gets removed and i land on the error page mentioned above Commented Sep 13, 2017 at 14:36
  • The below answer, then, is the one you want - do an automatic redirect to the ssl url. Commented Sep 13, 2017 at 17:45
  • Do i use a proxy_redirect inside location? Commented Sep 19, 2017 at 11:30
  • That's where I have mine, but it should work inside the server block and outside the location block. The location block will allow you to set it per individual call. Commented Sep 21, 2017 at 11:49
0

Well, using error_page seems to do the trick for me.

  server {
       listen 5000 ssl;
       server_name myserver.com;
       error_page 497 https://$host:5000$request_uri;
       ..
       ..
    }

To know more about the 497 check the "Error Processing" section in http://nginx.org/en/docs/http/ngx_http_ssl_module.html

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.