nginx https 没有 www 不会重定向到 https www

nginx https 没有 www 不会重定向到 https www

从 http www 和 http non www 重定向都重定向到 https www 但是如果我尝试从 https non www 重定向到 https www。它不起作用。这是我的服务器块

server {


                listen 443 ssl;
                 # catch all non-www domains
                server_name ~^(www\.)(?<domain>.+)$  ~(?<domain>.+)$;
                #return 301 https://www.$domain$request_uri;
                if ($host = $server_name) {
                        rewrite ^(.*) https://www.$server_name$request_uri? permanent;
                }
}

server {
        listen 80;
        server_name ~^(www\.)(?<domain>.+)$  ~(?<domain>.+)$;
        return 301 https://www.$domain$request_uri;
        }


}

我的代码有什么错误?

答案1

就这样

server {
    listen 80;
    listen 443 ssl;

    # ssl stuff, like ssl_certificate and ssl_certificate_key

    server_name domain.tld;
    return 301 https://www.domain.tld$request_uri;
}

server {
    listen 80;
    listen 443 ssl;

    # ssl stuff, like ssl_certificate and ssl_certificate_key

    server_name www.domain.tld;

    location / {
        # usual stuff here
    }
}

相关内容