Rails 应用程序使用 Nginx 反向代理背后的设备登录

Rails 应用程序使用 Nginx 反向代理背后的设备登录

我在 heroku 上部署了一个 rails 应用,我想使用 nginx 反向代理在子目录中访问它。除了设备登录/注册之外,几乎所有功能都正常,只有通过代理访问时才会出现这个问题。

例如,我的 rails 应用程序可以直接通过 heroku url 登录https://myapp.herokuapp.com/subdir

我想要的是能够登录我的自定义域https://example.com/subdir

这是我的 nginx 配置,我可以加载 rails 应用程序,但无法进行身份验证。

location /subdir/ {
  proxy_set_header  Host myapp.herokuapp.com;
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header  X-Forwarded-Proto https;
  proxy_set_header  X-NginX-Proxy true;
  proxy_ssl_server_name on;
  proxy_pass https://myapp.herokuapp.com/subdir/;
}

我尝试过带有和不带有上述代理标头的多种变体,但只需使用 Host 和 proxy_pass 一切仍能正常工作。

在尝试登录时,在服务器日志中我看到登录成功(我也可以创建一个帐户并将其填充到数据库中),但是我没有被重定向到经过身份验证的路径,而是被放回 /sign_up 路径,因为我没有经过身份验证

我在服务器日志中看到以下错误

Completed 401 Unauthorized
HTTP Origin header (https://example.com) didn't match request.base_url (https://myapp.herokuapp.com)

这个源标头似乎是问题所在,但我找不到解决方案。我觉得这可能是 nginx 中的一行配置更改?

相关内容