【问题标题】:Rerouting all http traffic to https with AWS ELB使用 AWS ELB 将所有 http 流量重新路由到 https
【发布时间】:2014-07-05 13:15:04
【问题描述】:

所以我查看了其他类似的问题,它们提供了解决方案,但由于某种原因,它们似乎都不起作用。所以,对于初学者来说,我的 ELB 是这样设置的

HTTP (incoming) -> HTTP (instance)
HTTPS (incoming) -> HTTP (instance)

所以两个流量都应该在端口 80 上进入。这很有效,因为当我使用 http://mydomain.comhttps://mydomain.com 访问我的网站时,即使我在端口 80 上只有一个 VirtualHost,它也能够显示。

问题在于尝试将所有 http 流量重写为 https。我过去常常根据端口来做这件事(检查是否!443 并重写为 https),但现在一切都进入 80,这将不起作用。所以我正在运行一个 Apache 服务器并有这个重写规则

RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [OR,NC]    # Added
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^/?(.*) https://mydomain.com%{REQUEST_URI} [L,R=301]

但它似乎永远不会起作用。我还缺少其他行吗?有没有办法检查它是否达到了那个条件?我尝试了 !httpshttp 作为条件,但都没有成功。

编辑:将我的 RewriteRule 稍微更改为现在的样子,但仍然无法正常工作。我添加了一个额外的条件来重写 www 并且 有效。 HTTP:X-Forwarded-Proto 要么不存在,要么负载均衡器没有设置

编辑:这个错误真的很愚蠢。我只是通过 SSH 连接到错误的实例。谢谢你容忍我的愚蠢

【问题讨论】:

    标签: apache mod-rewrite amazon-web-services amazon-elb


    【解决方案1】:

    要从 http 重写为 https,请使用以下规则。
    还要检查您的 mod 重写是否已启用并正常工作。

    RewriteEngine On
    # This will enable the Rewrite capabilities
    
    RewriteCond %{HTTPS} !=on
    # This checks to make sure the connection is not already HTTPS for "normal" conditions
    
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    # This checks the connection is not already HTTPS for AWS conditions
    
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    # This rule will redirect users from their original location, to the same location but using HTTPS.
    # i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
    # The leading slash is made optional so that this will work either in httpd.conf
    # or .htaccess context
    

    【讨论】:

    • 此解决方案将与 ELB 一起进行 https->http 转换,并将创建无限重定向循环。正确的解决方案(当存在负载均衡器时)实际上是查看 X-Forwarded-Proto 的 HTTP 标头,并相应地重定向。
    • 添加了上面缺失的条件:RewriteCond %{HTTP:X-Forwarded-Proto} !https
    【解决方案2】:

    这只是您的RewriteRule 无效。请参阅this post 了解它的外观。

    【讨论】:

    • 我试过了,但它不起作用。而且我知道我的 RewriteRule 是正确的,因为我也制定了一条路由 www 的规则,并且有效。有什么方法可以检查 HTTP:X-Forwarded-Proto 是否设置?
    猜你喜欢
    • 2017-10-16
    • 2018-04-15
    • 2014-04-27
    • 2017-09-30
    • 2016-09-26
    • 2016-07-27
    • 1970-01-01
    • 2015-06-27
    • 2012-04-19
    相关资源
    最近更新 更多