Server Bug Fix: How to redirect http access by ip or domain name to an https domain name?

Original Source Link

After configuring my Web server, when accessing via IP or domain name, https redirects are performed as follows:

http://192.168.10.5 to https://192.168.10.5

http://www.example.com to https://www.example.com

However, I want http://192.168.10.5 and http://www.example.com to be redirected to https://www.example.com.

Basic Nginx configuration:

server {
    listen       80 default_server;
    server_name  192.168.10.5 www.example.com;

    location / {
            rewrite     ^(.*)   https://$http_host$1 permanent;
    }
}

server {

    listen      443 http2 ssl;
    server_name www.example.com;

     root /var/www/example/html;

    ssl_certificate /etc/ssl/certs/mycert.crt;
    ssl_certificate_key /etc/ssl/private/mykey.key;

   } 

server {
  listen       80 default_server;
  server_name  192.168.10.5 www.example.com;

  location / {
        rewrite     ^(.*)   https://www.example.com$1 permanent;
  }
}

server {
  listen      443 http2 ssl;
  server_name 192.168.10.5;
  ssl_certificate /etc/ssl/certs/mycert.crt;
  ssl_certificate_key /etc/ssl/private/mykey.key;

  location / {
        rewrite     ^(.*)   https://www.example.com$1 permanent;
  }
}

server {
  listen      443 http2 ssl;
  server_name www.example.com;
  root /var/www/example/html;

  ssl_certificate /etc/ssl/certs/mycert.crt;
  ssl_certificate_key /etc/ssl/private/mykey.key;
} 

Tagged :

Leave a Reply

Your email address will not be published. Required fields are marked *