![nginx 使用 SSL 重定向域名](https://linux22.com/image/718866/nginx%20%E4%BD%BF%E7%94%A8%20SSL%20%E9%87%8D%E5%AE%9A%E5%90%91%E5%9F%9F%E5%90%8D.png)
我有一个新域名 (my-future-domain.com),我想用它来替换我当前的域名 (my-current-domain.com)。我在 Ubuntu 上使用 DigitalOcean Nginx 服务器,我认为我应该采取以下步骤才能用我的新域名替换当前域名,但我以前从未处理过域名迁移,这就是为什么我希望得到有关我计划采取的步骤的反馈。
1)将 A 记录指向与当前服务器关联的 IP,并将 CNAME 指向 A 记录
A= my-future-domain.com 指向 100.200.1.90
别名记录= www.my-future-domain.com 是 my-future-domain.com 的别名
2)使用 certbot 为 my-future-domain.com 和 www.my-future-domain.com 生成 SSL
sudo certbot --nginx -d my-future-domain.com -d www.my-future-domain.com
3)2: Redirect - Make all requests redirect to secure HTTPS access.
在 certbot 提示符下选择
4)server_name
为新域名添加新块并模拟当前服务器块
当前区块:
#This server block will redirect http:// to https://www.my-current-domain.com
server {
listen 80;
listen [::]:80;
server_name my-current-domain.com www.my-current-domain.com;
return 301 https://www.my-current-domain.com$request_uri;
}
#This server block will redirect https://my-current-domain.com to https://www.my-current-domain.com (expecting that you have a certificate for my-current-domain.com as well as www.my-current-domain.com
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-www.my-current-domain.com.conf;
include snippets/ssl-params.conf;
server_name my-current-domain.com;
return 301 https://www.$server_name$request_uri;
}
新的网站区块:
#This server block will redirect http:// to https://www.my-future-domain.com
server {
listen 80;
listen [::]:80;
server_name my-future-domain.com www.my-future-domain.com;
return 301 https://www.my-future-domain.com$request_uri;
}
#This server block will redirect https://my-future-domain.com to https://www.my-future-domain.com (expecting that you have a certificate for my-future-domain.com as well as www.my-future-domain.com
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-www.my-future-domain.com.conf;
include snippets/ssl-params.conf;
server_name my-future-domain.com;
return 301 https://www.$server_name$request_uri;
}
5)将流量从 my-current-domain.com 和 www.my-current-domain.com 重定向到 www.my-future-domain.com。我是否要从第 4 步更新“当前阻止”?
6)删除 my-current-domain.com 的 A 和 CNAME 记录
我可能遗漏了什么吗?
答案1
DNS 看起来正确。为了简化 DNS 请求,您可以将 my-current-domain.com 也设置为 www.my-future-domain.com 的 CNAME,而不仅仅是 www.my-current-domain.com。在旧域名到期之前,您无需删除 DNS;保留它是无害的。(保留它也将使任何仍然引用旧域名的内容受益。重定向应该会处理其余的事情。)
我将按如下方式配置您的服务器块。
server {
listen 80;
listen [::]:80;
server_name my-future-domain.com www.my-future-domain.com my-current-domain.com www.my-current-domain.com;
return 301 https://www.my-future-domain.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.my-future-domain.com;
…
您可以通过合并两个服务器块来清理代码。
如果你坚持你目前的计划,如果有人要求http://my-current-domain.com,他们将被重定向到https://www.my-current-domain.com然后再次重定向到https://www.my-future-domain.com。通过在初始服务器块中执行所有这些操作,您可以节省重定向。