【问题标题】:CallbackError with OmniAuth When Proxying Through Nginx通过 Nginx 代理时使用 OmniAuth 的 CallbackError
【发布时间】:2014-02-13 02:29:56
【问题描述】:

所以我使用 OmniAuth 和 GitHub 策略来处理我的项目的用户身份验证。直接访问 Rails 服务器时,一切正常。我最近设置了 Nginx 来处理我的开发前端和后端服务器之间的代理。现在,当我访问/auth/github 时,OmniAuth 会触发对 GitHub 的请求,但随后在回调中失败:

Started GET "/auth/github/callback?error=redirect_uri_mismatch" for 127.0.0.1 at 2014-01-22 11:54:35 -0800
I, [2014-01-22T11:54:35.365773 #13656]  INFO -- omniauth: (github) Callback phase initiated.
E, [2014-01-22T11:54:35.366091 #13656] ERROR -- omniauth: (github) Authentication failure! redirect_uri_mismatch: OmniAuth::Strategies::OAuth2::CallbackError, redirect_uri_mismatch
E, [2014-01-22T11:54:35.366149 #13656] ERROR -- omniauth: (github) Authentication failure! invalid_credentials: OmniAuth::Strategies::OAuth2::CallbackError, redirect_uri_mismatch

我已在 GitHub 上我的应用程序设置中将回调 URL 设置为正确的 URL,它显然可以正确地发出请求,只是使用了这个神秘的 redirect_uri_mismatch

这是我的 Nginx 服务器块:

server {
    listen       8080;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:9000;
    }

    location /api/ {
        proxy_pass http://localhost:3000;
    }

    location /auth/ {
        proxy_pass http://localhost:3000;
    }
}

虽然我是配置 Nginx 的相对菜鸟,但我真的看不出这不应该工作的任何充分理由。

【问题讨论】:

    标签: ruby-on-rails github nginx proxy omniauth


    【解决方案1】:

    好的,这里的问题是我没有正确设置我的标题。在我的 Nginx 配置中将以下内容添加到我的位置块修复了这个问题:

    location /api/ {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Client-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://localhost:3000;
    }
    

    【讨论】:

      【解决方案2】:

      天哪,我花了一个月的时间来解决这个问题。 我不断得到 - No route matches [GET] /auth/facebook

      Nginx 配置

      location @rails {
          proxy_set_header Host $http_host;
          proxy_set_header Client-IP $remote_addr;
          proxy_set_header X-Real-IP  $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_pass http://rails_app;
        }
      

      宝石文件

      #auth
      gem 'omniauth-facebook', '~> 8.0'
      gem 'omniauth', '~> 1.9.1' #this is important
      

      application.rb

      config.force_ssl = ENV['CLIENT_URL'].include?("https")
      

      omniauth.rb

      OmniAuth.config.logger = Rails.logger
      
      Rails.application.config.middleware.use OmniAuth::Builder do
        provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'],
                scope: 'email',
                callback_path: '/api/v1/auth/facebook/callback',
                image_size: 'large',
                secure_image_url: true,
                display: 'touch'
      end
      

      【讨论】:

      • 我不确定 CLIENT_URL 是什么意思,但设置 config.force_ssl = true 对 google oauth2 有用。
      • 这就是检测https的方法。 CLIENT_URL 是一个重定向域。
      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2014-02-14
      • 2011-12-14
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多