trestle HTTP Origin标头(https:..)与request.base_url(http://)不匹配

时间:2019-03-11 20:26:20

标签: ruby-on-rails ssl

嗨,我将我的服务器配置为接受ssl连接,我使用栈桥身份验证来管理身份验证。在localhost中工作完美,但是当我尝试在生产环境中收到错误

这是痕迹

Completed 200 OK in 2ms (Views: 1.5ms)
I, [2019-03-11T16:52:04.341111 #7304]  INFO -- : [fb212b57-7199-49df-b85f-3a0c8ff81ea8] Started POST "/admin/login" for 190.12.77.81 at 2019-03-11 16:52:04 +0000
I, [2019-03-11T16:52:04.341928 #7304]  INFO -- : [fb212b57-7199-49df-b85f-3a0c8ff81ea8] Processing by Trestle::Auth::SessionsController#create as HTML
I, [2019-03-11T16:52:04.342009 #7304]  INFO -- : [fb212b57-7199-49df-b85f-3a0c8ff81ea8]   Parameters: {"utf8"=>"✓", "authenticity_token"=>"UpwtvP0sNUrNqQazOh9BiLEv0uuOi75shw/leYNDJrPC9jpv7Qv9ShzZvFslBakHO+NjTtJPQ7bphiMbaBXzKg==", "email"=>"admin@silbia.com", "password"=>"[FILTERED]", "commit"=>"Login"}
W, [2019-03-11T16:52:04.342504 #7304]  WARN -- : [fb212b57-7199-49df-b85f-3a0c8ff81ea8] HTTP Origin header (https://wog.uye.pe) didn't match request.base_url (http://wog.uye.pe)

HTTP Origin标头(https://example.com)与request.base_url(http://example.com)轨道不匹配

我如何解决此问题。

致谢

2 个答案:

答案 0 :(得分:0)

在将Heroku和Cloudflare用于SSL时,我遇到了同样的问题。 对我有用的解决方案是在Cloudflare中将SSL设置为“完全”。

答案 1 :(得分:0)

您可以创建一个中间件:

# frozen_string_literal: true

require 'json'

class CloudflareProxy
  def initialize(app)
    @app = app
  end

  def call(env)
    return @app.call(env) unless env['HTTP_CF_VISITOR']

    env['HTTP_X_FORWARDED_PROTO'] = JSON.parse(env['HTTP_CF_VISITOR'])['scheme']
    @app.call(env)
  end
end

config/application.rb中使用:

config.middleware.use CloudflareProxy

CF-Visitor标头的引用:

https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers-

  

CF访客

     

仅包含一个称为scheme的键的JSON对象。该值与X-Forwarded-Proto的值相同(HTTP或HTTPS)。 CF-Visitor仅在使用灵活SSL时才相关。