gpt4 book ai didi

ruby-on-rails - AWS Elastic Load Balancer 有选择地启用 SSL

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:36 25 4
gpt4 key购买 nike

我目前有一个基于 Rails 的 Web 应用程序,它需要通过 HTTP 提供一小部分页面 - 但更喜欢通过 HTTPS 提供其余页面。在我当前的 AWS 设置中,SSL 在弹性负载均衡器处终止,并且与我的应用程序服务器的所有通信都通过 HTTP 进行。因此,像 Rack SSL Enforcer 这样的解决方案不合适。目前,我在每个页面中提供以下 JS 片段来处理重定向:

<% if should_be_ssl? %>
<script>
if (window.location.protocol != "https:"){
window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
}
</script>
<% else %>
<script>
if (window.location.protocol != "http:"){
window.location.href = "http:" + window.location.href.substring(window.location.protocol.length);
}
</script>
<% end %>

每次访问其中一个页面时,这都会导致相对显着的性能下降。有谁知道有选择地通过 SSL 服务某些页面并在负载平衡器级别控制它的方法吗?

最佳答案

ELB 本身目前不支持此功能,但 ELB 提供了一个 X-Forwarded-Proto header 。您可以检查它以查看来自客户端的请求是否通过 HTTPS。然后,如果需要,您可以提供重定向响应而不是页面内容。参见 this blog post来自 AWS 人员的更多信息。

你必须实现这个逻辑

  1. 使用中间件,例如 rack-ssl-enforcer

    查看 rack ssl enforcer 的文档,它似乎开箱即用地支持 X-Forwarded-Proto,所以你可能根本不需要做任何事情。你可以看到 the source header 得到尊重。

  2. 在您的应用程序中(可能带有重定向响应而不是在客户端)

  3. 在反向代理中,例如您的应用服务器和 ELB 之间的 haproxy

    acl is_http hdr(X-Forwarded-Proto) http
    acl account_login url_beg /account/login
    redirect scheme https code 301 if account_login is_http

根据您的配置,如果您在 ELB 和检查 header 的任何内容之间有任何其他反向代理,您可能需要配置它们以正确传递 X-Forwarded-Proto header 。参见 this issue ,例如。

关于ruby-on-rails - AWS Elastic Load Balancer 有选择地启用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com