通过Nginx代理时,带有OmniAuth的CallbackError [英] CallbackError with OmniAuth When Proxying Through Nginx
问题描述
因此,我将OmniAuth与GitHub策略配合使用来处理项目的用户身份验证.直接访问Rails服务器时,一切都会正常进行.我最近设置了Nginx来处理开发前端服务器和后端服务器之间的代理.现在,当我访问/auth/github
时,OmniAuth将请求触发到GitHub,但随后在回调上失败,
So I'm using OmniAuth with the GitHub strategy to handle user authentication for my project. When accessing the Rails server directly, everything works as it should. I recently set up Nginx to handle proxying between my development frontend and backend servers. Now when I visit /auth/github
, OmniAuth fires off the request to GitHub, but then fails on the callback with:
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
正确发出了请求.
I have set the callback URL in my application's settings on GitHub to the correct URL and it's obviously making the request properly, just with this mysterious redirect_uri_mismatch
.
这是我的Nginx服务器块:
Here's my Nginx server block:
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的相对菜鸟,但我真的看不出为什么它不起作用的任何充分理由.
I can't really see any good reason why this shouldn't be working, though I am a relative noob to configuring Nginx.
推荐答案
好,所以这里的问题是我没有正确设置标题.将以下内容添加到我的Nginx配置中的位置块中可以解决此问题:
Ok, so the issue here was that I wasn't setting my headers properly. Adding the following to my location blocks in my Nginx config fixed this:
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;
}
这篇关于通过Nginx代理时,带有OmniAuth的CallbackError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!