简体   繁体   中英

Rails redirect with proxy

I configured grunt-connect-proxy (based on http-node-proxy actually) to proxy my requests on localhost:3000 to my backend hosted myserver.com .

The backend app is running on Nginx (+ nginx passenger module) + passenger. There is no reverse proxy here.

The nginx configuration is like this:

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;
    }
}

It's actually like the one described here: https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu

The first request is OK: when I type localhost:3000 , I am actually on myserver.com. HTTP code = 200.

When I try to go to a protected page (with Devise), let's say http://localhost:3000/admin/articles , I get a HTTP code = 302 but I am redirected to myserver.com .

This means: I can log on myserver.com but not can't be logged on localhost:3000 .

How can I tell Rails or Devise to consider the proxied address?

You didn't really supply any data of your configuration, but I think your problem is that you didn't set the proxy_redirect

Add this line in the same block that contains the proxy_pass

proxy_redirect default;

The clue was here: https://github.com/rack/rack/blob/master/lib/rack/request.rb#L88

I had to send the HTTP_X_FORWARDED_HOST header with value = localhost:3000 . Then, it grunt proxy, you would have something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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