所以我使用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上我的应用程序设置中的回调网址设置为正确的网址,这显然正确地提出了请求,只是使用了这个神秘的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的相对noob。
答案 0 :(得分:4)
好的,这里的问题是我没有正确设置我的标题。在我的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;
}
答案 1 :(得分:0)
天哪,我花了一个月的时间来解决这个问题。
我不断得到 - 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;
}
Gemfile
#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