繁体   English   中英

Rails使用代理重定向

[英]Rails redirect with proxy

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

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

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考虑代理地址?

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

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

proxy_redirect default;

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

我必须发送HTTP_X_FORWARDED_HOST标头,其值为= localhost:3000 然后,它发出咕unt声的代理,您将获得类似以下内容的信息:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM