【问题标题】:Rails redirect with proxyRails 使用代理重定向
【发布时间】:2014-03-19 14:01:04
【问题描述】:

我配置了 grunt-connect-proxy(实际上基于 http-node-proxy)将我在 localhost:3000 上的请求代理到我托管的后端 myserver.com

后端应用在 Nginx(+ nginx 乘客模块)+ 乘客上运行。这里没有反向代理。

nginx的配置是这样的:

server {
    listen 80 default_server;

    root  /home/deployer/app/current/public;
    passenger_enabled on;

    access_log /var/log/nginx/app.access.log;

    if (-f $document_root/system/maintenance.html){
        rewrite ^(.*)$ /system/maintenance.html;
        break;
    }

   location ~* ^/assets/  {
      try_files $uri 404;
   }

    location ~* ^.+.(css|js|jpeg|jpg|gif|png|ico)$ {
        expires 30d;
    }

    location /nginx_status {
       stub_status on;
       access_log   off;
       allow 127.0.0.1;
       deny all;
    }
}

实际上就像这里描述的那样: https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu

第一个请求没问题:当我输入localhost:3000 时,我实际上是在myserver.com 上。 HTTP 代码 = 200。

当我尝试转到受保护的页面(使用 Devise)时,比如说 http://localhost:3000/admin/articles,我得到一个 HTTP 代码 = 302,但我被重定向到 myserver.com

这意味着:我可以登录myserver.com,但不能登录localhost:3000

我如何告诉 Rails 或 Devise 考虑代理地址?

【问题讨论】:

    标签: ruby-on-rails node.js nginx devise proxy


    【解决方案1】:

    您并没有真正提供任何配置数据,但我认为您的问题是您没有设置proxy_redirect

    将此行添加到包含proxy_pass 的同一块中

    proxy_redirect default;
    

    【讨论】:

      【解决方案2】:

      线索在这里:https://github.com/rack/rack/blob/master/lib/rack/request.rb#L88

      我必须发送值 = localhost:3000HTTP_X_FORWARDED_HOST 标头。 然后,它发出代理,你会得到类似的东西:

              {
                context: ['/'],
                host: 'myserver.com',
                port: 80,
                https: false,
                changeOrigin: true,
                xforward: true,
                headers: {
                  'x-forwarded-host': 'localhost:3000'
                }
              }
      

      【讨论】:

        猜你喜欢
        • 2012-06-03
        • 1970-01-01
        • 1970-01-01
        • 2020-05-12
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2021-06-26
        • 1970-01-01
        相关资源
        最近更新 更多