title: How to configure Nginx to redirect http 80 traffic to https 8443 port
body: <p>I have a Springboot application running in a cloud machine on port 8443.  In the same machine I have a Nginx server. </p>
  
  <p>Today I access <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a> and it works fine but if I type  www.example.com and try to access it is not redirecting to <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a>.</p>
  
  <p>In other words, all http 80 traffic should be redirected to https 8443</p>
  
  <p>Here are my configuration (Springboot app + Nginx) </p>
  
  <p><strong>Springboot application.properties</strong></p>
  
  <pre><code>server.port=8443
  security.require-ssl=true
  server.ssl.key-store=/etc/letsencrypt/live/www.example.com/keystore.p12
  server.ssl.key-store-password=www.example.com
  server.ssl.keyStoreType=PKCS12
  server.ssl.keyAlias=myAlias
  </code></pre>
  
  <p><strong>Nginx /etc/nginx/nginx.conf</strong></p>
  
  <pre><code>pid /run/nginx.pid;
  
  events {
      worker_connections 768;
  }
  
  http {
  
      log_format formatWithUpstreamLogging '[$time_local] $remote_addr - $remote_user - $server_name to: $upstream_addr: $request';
  
      #main log format
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                 '$status $body_bytes_sent "$http_referer" '
                                 '"$http_user_agent" "$http_x_forwarded_for"';
  
      access_log  /var/log/nginx/access.log main;
      error_log   /var/log/nginx/error.log;
  
      server {
  
          listen 80;
  
          server_name www.example.com example.com;
  
          ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
          ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  
          # managed by Certbot
          location / {
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header Host $host;
                  proxy_set_header X-NginX-Proxy true;
                  proxy_pass https://localhost:8443/;
                  proxy_redirect http://localhost:8443/ https://localhost:8443/;
         }
  
      }
  
  }
  </code></pre>
  
  <p>Could anyone help me on this?</p>
  
  <p>Thanks in advance</p>