通过判断$scheme是否为http,配合rewrite实现用户访问http自动跳转到https

[root@www conf.d]# vi abc.conf 
server {
   listen 80;
   listen 443 ssl;
   server_name www.abc.cn;
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
   ssl_certificate /software/nginx/certs/www.abc.cn.pem;
   ssl_certificate_key /software/nginx/certs/www.abc.cn.key;
   ssl_session_cache shared:sslcache:20m;
   ssl_session_timeout 10m;

   location / {
   
   if ( $scheme = http ) {
   rewrite ^/(.*)$ https://www.abc.cn/$1 redirect;
   }
   root /data/nginx/html/abc;
   }
}

访问http://www.abc.cn测试,可以看到自动跳转到了https

nginx下配置用户http自动跳转https_nginx