【问题标题】:Want to https://www.example.org to https://example.org in Nginx想在 Nginx 中将 https://www.example.org 转为 https://example.org
【发布时间】:2021-08-07 09:50:59
【问题描述】:

我想在 Nginx 中将 https://www.example.com 重定向到 https://example.com

我尝试使用以下代码执行此操作,但我必须重定向多个域,因此无法在返回行中提及特定的“example.com”。我有大约 30 个不同的域需要这样做。

我已经尝试了许多建议的解决方案,但所有解决方案都使用一个不符合我要求的域。就像如果有域(example.com、example.com、xyz.com 等)都需要重定向到 https://reportivedomain(non-www)

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

    # redirects www to non-www. wasn't work for me without this server block
    return 301 $scheme://example.com$request_uri;
}

更新:

server {
    server_name "www.(.+?\.\w+)" ;
    return 301 https://$1$request_uri;
}

我尝试了上面的代码,但仍然出现错误“NET::ERR_CERT_COMMON_NAME_INVALID”。

该网站正在使用下面

example.com
http://example.com
https://example.com

但不是将 www 添加到域,SSL 证书将颁发给非 www 域

【问题讨论】:

    标签: nginx ssl web server dns


    【解决方案1】:

    您可以使用下面的配置进行重定向。

    单域

    server {
        listen 443 ssl http2;
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }
    

    多个域

    server {
        listen 443 ssl http2;
        server_name "~^www\.(.*)$" ;
        return 301 $scheme://$1$request_uri;
    }
    

    source

    【讨论】:

    • 我尝试了上面的代码并在下面修改它,但仍然出现错误“NET::ERR_CERT_COMMON_NAME_INVALID”。该站点正在使用下面的 example.com example.com example.com 但不是将 www 添加到域,SSL 是非 www 域服务器的问题 { server_name "www.(.+?\.\w+)" ;返回 301 https://$1$request_uri; }
    • 那是ssl证书问题,你的ssl证书必须包含你使用的所有域名,包括www和非www。
    • 当我尝试运行站点example.com 时,它会重定向到example.com,但是当我尝试使用example.com 时出现错误“NET::ERR_CERT_COMMON_NAME_INVALID”。这种情况没有任何变化
    • 您证书上的通用名称与安装它的域不匹配,请确保您的 ssl 证书包含您的域名。
    • 证书有“example.com”但没有“www.example.com”
    猜你喜欢
    • 2021-07-11
    • 2016-05-31
    • 1970-01-01
    • 2011-08-10
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    相关资源
    最近更新 更多