简体   繁体   English

nginx https 重定向添加 www

[英]nginx https redirect adds www

I have two server blocks in my nginx configuration:我的 nginx 配置中有两个服务器块:

server {
    listen 80;
    server_name domain.com;
    return 301 https://domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name domain.com;
    <ssl stuff>
    <root directory>
}

I'm basically redirecting all HTTP traffic to HTTPS using the first server block.我基本上使用第一个服务器块将所有 HTTP 流量重定向到 HTTPS。 I'm hardcoding the redirect domain name because I want to explicitly avoid redirecting to htps://www.domain... - I want https://domain ...我对重定向域名进行了硬编码,因为我想明确避免重定向到 htps://www.domain ... - 我想要https://domain ...

When I request non-www HTTP domain, nginx correctly redirects to non-www HTTPS domain当我请求非 www HTTP 域时,nginx 正确重定向到非 www HTTPS 域

However, when I request the www HTTP domain, nginx is not redirecting to the non-www HTTPS domain.但是,当我请求 www HTTP 域时,nginx不会重定向到非 www HTTPS 域。 Somehow, it adds a www to the HTTPS redirect even when I explicitly specified not to.不知何故,即使我明确指定不这样做,它也会向 HTTPS 重定向添加 www。

WHY?为什么?

Just looking at this without testing, I would say it's not picking up www.domain.com because that's not specified in the config.只是在没有测试的情况下查看这个,我会说它没有选择 www.domain.com 因为它没有在配置中指定。 I belive you need a separate specification for www, or use a regex.我相信您需要一个单独的 www 规范,或者使用正则表达式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM