为什么 www.domain.com 没有重定向到 domain.com?

为什么 www.domain.com 没有重定向到 domain.com?

我想www.lizardgizzards.com重定向到https://lizardgizzards.com

但相反,它会重定向到https://www.lizardgizzards.com

这是我的重定向配置:

server {
        listen 80;
        listen [::]:80;

        server_name lizardgizzards.com www.lizardgizzards.com;

        if ($host = 'www.lizardgizzards.com') {
                return 301 https://lizardgizzards.com;
        }

        if ($host = 'lizardgizzards.com') {
                return 301 https://$host$request_uri;
        }
}

我的 SSL 配置:

server {
        root /var/www/html/lizardgizzards.com;

        index index.html index.htm index.nginx-debian.html;

        server_name lizardgizzards.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        listen [::]:443 ssl ipv6only=on default_server; # managed by Certbot
        listen 443 ssl default_server; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/lizardgizzards.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/lizardgizzards.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

它似乎正在使用以下dig命令:

ubuntu@mars:/var/www/html$ curl -I www.lizardgizzards.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Date: Thu, 09 May 2019 03:52:45 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: http://lizardgizzards.com

但我的浏览器似乎没有收到备忘录。即使在隐身窗口中,它也会转到错误的 URL:https://www.lizardgizzards.com

答案1

所以问题是我之前接受了 Chrome 中针对无效证书的安全异常......

在 Chrome 中,我选择重新启用 SSL 警告,现在重定向正常运行。

答案2

检查您浏览器开发者栏中的网络选项卡(在 Chrome 或 Firefox 中按 [F12],在 Safari 中按 [ALT][CMD][i] 并更改为网络)。激活“保留日志”和“禁用缓存”可获得更好的效果。

再次开始浏览您想要的 URL 并观察浏览器正在做的事情。

我假设您将看到从 http 到 https 位置的 307 重定向。这意味着您的浏览器将使用内部重定向。您的浏览器不会访问服务器并处理服务器的响应,而是拥有一个支持 https 协议的网站内部列表。它将内部重定向这些网站的请求到 https,而不是使用 http。

此类网站会发送 HSTS 标头,以告知浏览器使用 https 的偏好。此标头的有效期为秒。实际上,一旦发送,就很难从浏览器中删除此偏好。

相关内容