在Nginx站点配置中增加一行
rewrite ^(.*)$ https://$host$1 permanent;
Nginx配置有2中情况, 80 443在同一个server{} 和不同server{}
第一种 ( 80端口 和 443端口是分开的两个server{}
):
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/www.example.com.crt;
ssl_certificate_key /path/www.example.com.key;
}
第二种 (80 443端口在同一个server{}
):
server {
listen 80;
listen [::]:80;
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/www.example.com.crt;
ssl_certificate_key /path/www.example.com.key;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
}