gpt4 book ai didi

ruby-on-rails - 使用 nginx 为 rails 中的登录用户强制 ssl

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:40 24 4
gpt4 key购买 nike

我的堆栈看起来像这样 nginx -> thin -> rails。在我的 Rails 应用程序中,我的 applicaton_controller.rb 中有:

  if (!Rails.env.development?)
before_filter :force_ssl
end

# Force logged in users to use SSL
def force_ssl
if current_user && request.protocol != "https://"
redirect_to :protocol => "https://"
end
end

问题是所有请求看起来都像 http,因为 nginx 处理 ssl 并转发到 thin 并导致无限重定向循环。在这种情况下为登录用户设置 ssl 的正确方法是什么?

最佳答案

您可以使用 proxy_set_header设置自定义 header 的指令,告诉您的后端请求来自安全前端。

例子:

for 80:
proxy_set_header X-SSL 0;

for 443:
proxy_set_header X-SSL 1;

或全局

proxy_set_header X-Forwarded-Proto $scheme;

关于ruby-on-rails - 使用 nginx 为 rails 中的登录用户强制 ssl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856688/

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