如何将 http://www 重定向到 https://www - nginx

how to redirect http://www to https://www - nginx

我要将 http://www.example.com 重定向到 https://www.example.com。在 nginx 服务器块中,我有以下代码段:

server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.$host$request_uri;

}

example.com 情况下,它可以正常工作并将 http://example.com 重定向到 https://www.example.com,但在 http://www 情况下,它无法正确重定向并将浏览器重定向到 [=17] =].我该如何解决这个问题?

使用$server_name代替$host,例子:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}