tobinjim
tobinjim

Reputation: 1852

RoR3 redirects not including RAILS_RELATIVE_URL_ROOT

On my public-facing Apache2 server access to my Rails 3 app is via a sub-URI, like

https://www.myserver.com/myapp/controller...

that server is configured for reverse proxy to another Apache2 server hosting the Rails app via Passenger:

http://intranet.server.com/myapp

In my VirtualHost settings for the intranet server I have

SetEnv RAILS_RELATIVE_URL_ROOT "/myapp"

In my config/environments/production.rb file I uncommented the line

config.log_level = :debug

This way, the very first executing method in my app issues:

logger.debug { "The environment variable RAILS_RELATIVE_URL_ROOT is presently #{ENV['RAILS_RELATIVE_URL_ROOT']}" }

and sure enough, in the production log file you see:

The environment variable RAILS_RELATIVE_URL_ROOT is presently /myapp

However, the next line in the log is, which is obviously what the browser goes to:

Redirected to https://www.myserver.com/controller/action

I learned about this environment variable here on Stackoverflow, so that's one person for whom it worked, but one other person chimed it didn't to which another said it did. So with my experience, we're now at a tie. Am I missing something?

Upvotes: 4

Views: 3117

Answers (4)

Nathan Wallace
Nathan Wallace

Reputation: 2244

I found this thread on the Rails Github useful: https://github.com/rails/rails/issues/5122

I ended up doing the following in config/routes (I configured relative_url_root via the RAILS_RELATIVE_URL_ROOT environment variable):

Rails.application.routes.draw do |*args|
  scope Rails.application.config.relative_url_root || "/" do
    ...
  end
end

Upvotes: 0

james tan
james tan

Reputation: 51

Wow, thanks a lot, i finally figure it out.

it seems that we need to do proxy to the exact relative url on destination rails:, the same matching uri on apache.

eg: http://localhost/example/ to http://localhost:3000/example/

these example below has been tested on Rails 4:

so in proxy httpd.conf:

ProxyRequests Off
ProxyPreserveHost On
< Proxy *>
Order deny,allow
Allow from all
< /Proxy>

ProxyPass /example http:// localhost:3000/example

ProxyPassReverse /example http:// localhost:3000/example

Then next, in rails config.ru:

require ::File.expand_path('../config/environment', FILE)
#run Rails.application
map ActionController::Base.config.relative_url_root do
run Examplerails::Application
end

map "/" do
run Examplerails::Application
end

Finally, in config/application.rb

module Examplerails
class Application < Rails::Application
config.action_controller.relative_url_root = "/example"
end
end

Please note closely on trailing /.
Some have trailing slash while others dont need it.
Also remove some spacing between tag and url in this code above,
im trying to format it to be displayed here.

Upvotes: 1

tobinjim
tobinjim

Reputation: 1852

I figured out my error. The reverse proxy settings on the external server referenced just the hostname of the internal server and left off the path to my RoR3 app.

Upvotes: 0

Michael Johnston
Michael Johnston

Reputation: 5320

To the best of my knowledge, being able to use the ENV var directly was changed quite some time ago:

https://rails.lighthouseapp.com/projects/8994/tickets/1946-setting-a-relative-root-url-via-a-web-server-not-possible-anymore

So in environment.rb (or an initializer) you could set config.action_controller.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']

Upvotes: 1

Related Questions