Jas*_*don 6 ssl ruby-on-rails hsts
我运行一个应用程序,从单个应用程序和服务器托管来自多个域的网站.我正在将其中一些域名移至SSL,但其他域名则保留在http.我正在运行Rails 4.x. 我相信我不能只使用
config.force_ssl = true
Run Code Online (Sandbox Code Playgroud)
因为那将为所有域实现它,我不想要.
我知道在ApplicationController中我可以做类似的事情
force_ssl if: :ssl_required?
def ssl_required?
return [secure_domain1, domain2, domain3].include? request.domain
end
Run Code Online (Sandbox Code Playgroud)
但据我了解,这并没有实现HSTS或安全cookie.我的两个问题是:
如果没有简单的方法来启用HSTS或安全cookie,并且让这些值得麻烦,我可以随时拆分我的应用并将其托管在两个不同的服务器上,其中一个实例包含所有https域,另一个实例仅包含http域.
谢谢你的想法
使用 Rails 来做到这一点实际上并不是一个坏主意,但是正如评论所指出的那样,使用 NGINX 来做到这一点会更好。如果你真的想追求 Rails 解决方案,你可以同样做类似的事情def:
force_ssl if: :ssl_required?
def ssl_required?
if [secure_domain1, domain2, domain3].include? request.domain
#HSTS for Rails <=4
response.headers['Strict-Transport-Security'] = 'max-age=315360000; includeSubdomains; preload'
#HSTS for Rails >=5
response.set_header('Strict-Transport-Security', 'max-age=315360000; includeSubdomains; preload')
cookies[:secure] = true
true
else
false
end
end
Run Code Online (Sandbox Code Playgroud)
您始终可以将 HSTS 标头调整为所需的值max-age,或者使用更惯用的方法放置#{365.days.to_i}而不是简单的字符串标头。