【问题标题】:Relative link (from https) gives 301 Permanently Moved (to http)相对链接(来自 https)给出 301 Permanently Moved (to http)
【发布时间】:2016-06-05 15:13:31
【问题描述】:

我拥有一个域名(我们称之为example.org),并设置了一个从foo.example.org 指向AWS ELB FooBar-Load-Balancer-123456789.us-east-1.elb.amazonaws.com 的CNAME。该 ELB 的端口配置为 443 (HTTPS, ACM Certificate <GUID>) forwarding to 80 (HTTP)。 ELB 后面的唯一 EC2 实例运行 Docker 映像,在端口 80 上公开 Apache。

当我在网络浏览器中打开 https://foo.example.org 时,一切正常 - 页面按预期加载。如果我导航到https://foo.example.org/path,它同样会正确加载。但是,如果页面包含<a href="path">,则单击浏览器会尝试加载http://foo.example.org/path,这(正确)会给出错误-Chrome 中的“ERR_CONNECTION_REFUSED”,Firefox 中的“无法连接”。

检查 Chome 开发工具中的网络活动,我看到一个对https://foo.example.org/path 的初始请求,结果是301 Moved Permanently (from cache),位置为http://foo.example.org/path。这显然是导致浏览器行为的原因 - 这是我的服务器(它认为自己在 HTTP 上提供服务 - 或者至少是端口 80)、我的 ELB 或网站的 html 本身上的错误配置?

我想我可以通过使用绝对路径来解决这个问题,但是,考虑到我希望能够在本地启动 Docker 映像(在我的浏览器中打开 <IP Address>/path)以在推送更改之前进行测试,那并没有听起来像是一个真正的解决方案。

编辑:受this 的启发,我在新的 Chrome 隐身模式窗口和清除历史记录后的 Chrome 中检查了行为 - 在所有情况下都是相同的行为。

【问题讨论】:

    标签: apache redirect ssl https load-balancing


    【解决方案1】:

    发布此作为答案,因为我在技术上已畅通无阻,尽管我仍然非常感谢知识渊博的人解释为什么会出现这些症状。

    对 curl 的一些调查让我明白了这一点:

    $ curl -v https://foo.example.org/path
    *   Trying 52.0.230.252...
    * Connected to foo.example.org (52.0.230.252) port 443 (#0)
    * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    * Server certificate: example.org
    * Server certificate: Amazon
    * Server certificate: Amazon Root CA 1
    * Server certificate: Starfield Services Root Certificate Authority - G2
    > GET /path HTTP/1.1
    > Host: foo.example.org
    > User-Agent: curl/7.43.0
    > Accept: */*
    >
    < HTTP/1.1 301 Moved Permanently
    < Content-Type: text/html; charset=iso-8859-1
    < Date: Sun, 05 Jun 2016 18:35:58 GMT
    < Location: http://foo.example.org/path/
    < Server: Apache/2.4.7 (Ubuntu)
    < Content-Length: 336
    < Connection: keep-alive
    <
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>301 Moved Permanently</title>
    </head><body>
    <h1>Moved Permanently</h1>
    <p>The document has moved <a href="http://foo.example.org/path/">here</a>.</p>
    <hr>
    <address>Apache/2.4.7 (Ubuntu) Server at foo.example.org Port 80</address>
    </body></html>
    * Connection #0 to host foo.example.org left intact
    $ curl https://foo.example.org/path/ # Note trailing slash
    <expected html>
    

    所以它看起来像making a request to apache for a directory without a trailing slash(“目录需要尾部斜杠,所以mod_dir 发出重定向到http://servername/foo/dirname/.”)。这就解释了为什么 301 响应中的 Location 标头使用 http:// - apache 服务于 http://,所以“不知道更好”。我想我可以通过使我的锚标记显式链接到带有斜杠的 href 来解决这个问题。

    为什么 Apache 是这样配置的?为什么不直接“内部”自动解析到适当的位置,而不必往返 301 响应?而且,最重要的是——我有没有更好的方法来解决这个问题?是否可以将 ELB 配置为重写 Location 标头(我猜不是——我不是 InfoSec 专业人士,但这让我觉得这是一个等待发生的漏洞)? Apache可以吗?

    【讨论】:

      猜你喜欢
      • 2014-12-30
      • 2012-01-09
      • 2017-02-28
      • 1970-01-01
      • 2014-07-05
      • 2017-06-13
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      相关资源
      最近更新 更多