Google OAuth 2.0 的 Nginx 代理

Nginx proxy with Google OAuth 2.0

我有一个 Ubuntu 14.04 服务器,我有一个在该服务器上 localhost:3000 运行的流星应用程序。我服务器的 public FQDN 是 sub.example.com。流星应用程序使用 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 配置文件中的 header,但没有成功。

我显然漏掉了什么。

您应该重写 Location headers 您的后端发送到 http://wiki.nginx.org/HttpProxyModule#proxy_redirect 中描述的 Nginx,因此:

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

另一个选项,也适用于 popup-style 登录,是在启动时为 Meteor 设置 ROOT_URL 环境变量,如下所示:

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