grantperry
grantperry

Reputation: 149

Proxied Rails app relative_url_root not prefixing url helpers

So I have Nginx proxying a rails app with the following config

location ^~ /admin {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://admin/;
}

which has the affect of re-writing all requests to http://localhost/admin/XXX/YYY/ZZZ => http://admin/XXX/YYY/ZZZ

so next I ran into the issue of assets urls not pointing at the proxied app, so config.relative_url_root = '/admin' did the trick

Next problem is every link generated by link_to returns a link based at / not /admin which I would believe config.relative_url_root would change... Now I am sort of correct, If i go to rails console then include Rails.application.routes.url_helpers then call say root_path or management_path, I receive /admin and /admin/management. Perfect!

Now this is where I get rather confused. All the routes generated in my views disregard the prefix set by config.relative_url_root... Why is this configuration option working in the console environment but not in the view environment?

config.relative_url_root is located in my application.rb

Rails 5.1.5

Upvotes: 2

Views: 1250

Answers (1)

grantperry
grantperry

Reputation: 149

This resolution is not quite what I was hoping for but in the meantime it works.

/config.ru

map ENV['RAILS_RELATIVE_URL_ROOT'] || "/" do
  run Rails.application
end

/nginx.conf

location ^~ /admin {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://HC_admin/admin;
}

All that changed is that the app itself now thinks it is running at /admin or whatever you set RAILS_RELATIVE_URL_ROOT to. This in my mind defeats the purpose of parts of your app being separated as you still need to configure where its mounted.

Found some instructions in a gist, here

Upvotes: 1

Related Questions