DNS 记录将 www 重定向到非 www [英] DNS Records Redirect www to non-www

查看:35
本文介绍了DNS 记录将 www 重定向到非 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Namecheap 域和 Vultr 托管.

I'm using Namecheap Domains and Vultr Hosting.

我正在尝试将 DNS www 重定向到非 www.

I'm trying to redirect DNS www to non-www.

www.example.com 到 example.com

www.example.com to example.com

我联系了 Vultr 并询问如何使用他们的 DNS 管理器执行此操作,他们说他们不会提供帮助,因为它是自我管理的.所以我联系了 Namecheap,他们说他们不会提供帮助,因为他们无法访问 Vultr 的 DNS 管理器,不会告诉我我向他们展示的记录是否正确,我需要联系 Vultr.所以我处于无休止的支持循环中.

I contacted Vultr and asked how to do this with their DNS Manager, they said they would not help as it is self-managed. So I contacted Namecheap, they said they would not help becuase they don't have access to Vultr's DNS Manager, would not tell me if the records I showed them are correct, and I would need to contact Vultr. So I am in an endless support loop.

Vultr DNS 管理器

我关注了这个答案,了解如何设置 CNAME 以重定向到非 www.

I followed this answer on how to set up a CNAME to redirect to non-www.

Type   | Name | Data         | Seconds
--------------------------------------
A      |      | ipv4 address | 300
AAAA   |      | ipv6 address | 300
CNAME  | .    | example.com  | 300
CNAME  | www  | example.com  | 300

等了一晚上才传播,www仍然可以访问,并且不会重定向.

After waiting all night for it to propgate, the www can still be visited and does not redirect.

它不允许我创建另一个 A 记录,只能创建 CNAME.它说:

It does not allow me to make another A record, only CNAME. It says:

Unable to add record: A CNAME record is not allowed to coexist with any other data. 

<小时>

NGINX

我关注了本指南 并尝试使用站点可用配置重定向它.Http 和 Https 工作,但 www 不重定向到非 www.

I followed this guide and tried redirecting it with sites-available config. Http and Https work, but www does not redirect to non-www.

server {
    # Redirect http to https
    listen 80;
    return 301 https://$host$request_uri;
}

server {
    # Redirect www to non-www
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 443 ssl default_server;

    ssl on;
    ssl_certificate /etc/nginx/ssl/cert_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/example_com.key;
    ssl_protocols  TLSv1.1 TLSv1.2;

    server_name example.com;
    ...

推荐答案

DNS 无法将您的 www 站点重定向到非 www 站点.DNS 的唯一目的是使用 AAAAACNAME 记录将 www 和非 www 指向您服务器的 IP 地址(它使差别不大).nginx 配置负责执行从 www 到非 www 的重定向.

DNS cannot redirect your www site to non-www. The only purpose of DNS is to point both www and non-www to your server's IP address using A, AAAA or CNAME records (it makes little difference). The nginx configuration is responsible for performing the redirect from www to non-www.

您的第二个服务器块旨在从 www 重定向到非 www,但目前仅处理 http 连接(在端口 80 上).

Your second server block is intended to redirect from www to non-www, but currently only handles http connections (on port 80).

您可以移动默认服务器并使用它来将所有内容重定向到预期的域名.例如:

You can move the default server and use that to redirect everything to the intended domain name. For example:

ssl_certificate /etc/nginx/ssl/cert_chain.crt;
ssl_certificate_key /etc/nginx/ssl/example_com.key;

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    ...
}

假设您拥有 www 和非 www 域名的通用证书,您可以将 ssl_ 指令移动到外部块中,并允许它们继承到两个服务器块中(如如上所示).

Assuming that you have a common certificate for both the www and non-www domain names, you can move the ssl_ directives into the outer block and allow them to be inherited into both server blocks (as shown above).

有关更多信息,请参阅本文档.

See this document for more.

这篇关于DNS 记录将 www 重定向到非 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆