如何将裸 "example.com" 域重定向到 "https://example.com"?

How do you redirect a bare "example.com" domain to "https://example.com"?

标题真的说明了一切。我无法让我的域从 site.com 重定向到 http://example.comhttps://example.comhttps://www.example.com.

一样有效

我的 nginx conf 如下,删除了敏感路径。这是我所有 nginx 设置的 GIST link,它目前是我整个 nginx configuration.

中唯一启用的域

本地与服务器上的控制台输出:

rublev@rublevs-MacBook-Pro ~
• curl -I rublev.io
^C
rublev@rublevs-MacBook-Pro ~
• ssh r
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-75-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

78 packages can be updated.
0 updates are security updates.

Last login: Fri May 12 16:41:35 2017 from 198.84.225.249
rublev@ubuntu-512mb-tor1-01:~$ curl -I rublev.io
HTTP/1.1 200 OK
Server: nginx/1.10.0 (Ubuntu)
Date: Fri, 12 May 2017 16:41:43 GMT
Content-Type: text/html
Content-Length: 339
Last-Modified: Thu, 20 Apr 2017 20:47:12 GMT
Connection: keep-alive
ETag: "58f91e50-153"
Accept-Ranges: bytes

我束手无策,我真的不知道现在该做什么,我花了几周的时间试图让它工作。

添加服务器

server {
    server_name example.com;
    return 301 https://example.com$request_uri;
}

LetsEncrypt 默认使用 http,为了安全起见,最好保留它访问 well-known 的能力以应对 acme 挑战。您似乎也没有指向正确的路径,除非您将 webroot 更改为 /home/rublev/sites/rublev.io?

我会尝试像这样重写您的默认服务器,而不是直接重定向到您的 https 等效服务器。此外,它可以让您更轻松地测试这种奇怪的行为。

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name rublev.io www.rublev.io;

  location / {
    return 301 https://$server_name$request_uri;
  }

  location ~ /.well-known {
    # Apparently you changed your webroot,
    # just make sure that the file is created and accessible
    root /home/rublev/sites/rublev.io/.well-known;
  }
}

此外,了解您需要两个不同的证书或一个接受您的域的 www 形式的证书非常重要。如果不是这种情况,那很可能就是您出现问题的原因。要生成包含两个域的证书,您可以 运行 以下命令:

sudo ./certbot-auto certonly --standalone -d rublev.io -d www.rublev.io --dry-run

我还会注释掉您的 jenkins 服务器和配置,以防万一它弄乱了您的主要服务器。注意:从 this question 开始,您可以使用 proxy_redirect http:// $scheme://; 代替您当前正在使用的表格。它不应该影响另一台服务器,但我更愿意在这些奇怪的情况下确定。


另一件事,可能是通向另一种解决方案的途径,是考虑只选择一种形式的域(有或没有 www),并从 "wrong" 重定向用户到 "right" 一个。这将使您获得更一致的 url,对 SEO 和共享您的链接的人更好。