Nginx代理与Google OAuth 2.0

6

我有一台运行Ubuntu 14.04的服务器,其中安装有一个在该服务器上的localhost:3000运行的Meteor应用程序。 我的服务器的公共FQDN是sub.example.com。 Meteor应用程序使用Google OAuth 2.0进行身份验证,以下是我在Google API控制台中配置的内容:

URI REDIRECTION  
http://sub.example.com/_oauth/google
http://sub.example.com/_oauth/google?close
 ORIGINES JAVASCRIPT 
http://sub.example.com

我的Nginx配置文件如下所示:
server {
    listen 80 default_server;
    server_name sub.example.com www.sub.example.com;
    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass http://localhost:3000;
    }
}

代理服务器运行正常,我可以通过访问“sub.example.com”来访问我的Meteor应用程序。但是当我尝试使用Google OAuth 2.0时,在弹出窗口中我得到了以下信息:
Error: redirect_uri_mismatch
The redirect URI in the request: http://localhost:3000/_oauth/google?close did not match a registered redirect URI.

我尝试修改了nginx配置文件中的标题,但没有成功。显然我缺少某些步骤。

你正在从http://localhost:3000/_oauth/google?close发送请求,请将其放在重定向URI中。在云控制台上进行操作。 - Linda Lawton - DaImTo
据我了解,这不起作用是因为用户浏览器无法访问服务器的本地主机。但是,如果Meteor应用程序在我的笔记本电脑上,我可以这样做,并且它会工作。谢谢。 - mthpvg
1个回答

6

您需要重写后端发送到Nginx的Location头,可以参考http://wiki.nginx.org/HttpProxyModule#proxy_redirect进行操作,具体步骤如下:

proxy_redirect http://localhost:3000/_oauth/google http://sub.example.com/_oauth/google;

另一个选项,也适用于弹出式登录,是在Meteor启动时设置ROOT_URL环境变量,方法如下:

ROOT_URL="http://sub.example.com" PORT=3000 node main.js

“export ROOT_URL="http://sub.example.com"” 对我来说是正确的,我甚至明白为什么。只是添加代理重定向没有起作用。谢谢汉斯·Z. - mthpvg
1
是的,proxy_redirect 对于非弹出窗口/全浏览器重定向环境有效。 - Hans Z.
1
太棒了!节省了我一两个小时的时间!有关 Meteor 使用的全面环境变量集,请参见:http://www.meteorpedia.com/read/Environment_Variables - pkk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接