如何将一个简单的“example.com”域重定向到“https://example.com”?

时间:2017-05-10 02:35:35

标签: http redirect nginx https ssl-certificate

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

我的nginx conf如下,删除了敏感路径。这是我所有nginx设置的GIST链接,它是我整个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

我在我的智慧结束时,我真的不知道现在该做什么,我花了几周的时间试图让它发挥作用。

2 个答案:

答案 0 :(得分:3)

LetsEncrypt默认使用http,为了安全起见,最好让它能够访问极限挑战的well-known。除非您将您的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),并从"错误&#34重定向用户;到"对"一。这将允许您获得更一致的网址,更好的搜索引擎优化和人们分享您的链接。

答案 1 :(得分:1)

添加服务器

server {
    server_name example.com;
    return 301 https://example.com$request_uri;
}
相关问题