1

With Phusion Passenger, there are specific directives to use in the nginx virtual host configuration that allow Rails apps to be hosted on sub-urls. This is described in the Phusion Passenger documentation.

With Puma, I am using sockets. My nginx config includes:

upstream subapp {
    server unix:<path_to_subapp_folder>/shared/sockets/puma.sock fail_timeout=0;
}

server {
...
    location /subapp {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://subapp;
    }
}

The sub app receives the GET as '/subapp' but of course doesn't know what to do with it. If I use URL rewriting and pass it everything in the URL after '/subapp', it renders the page, but all links, including asset paths, do not include '/subapp', and as such are invalid.

How does one configure Rails so that it prepends '/subapp' to the paths? I did manage to use a scope in routes.rb (scope(:path => (is_dev? ? '' : '/subapp')) do) but this seemed artificial and it didn't prepend to asset paths. I imagine there is a way to configure nginx/Puma (as one can with Passenger) to deal with this situation.

danebez
  • 353
  • 4
  • 15
  • I have just discovered http://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root. When I use "config.relative_url_root", the assets are now found at the correct URLs. However, my links are still not being prefixed with /subapp. Will continue investigating. – danebez Jun 05 '15 at 10:56

2 Answers2

3

The solution required the following change to config.ru:

if ENV['RAILS_ENV'] != 'production'
    run SubApp::Application
else
    # production
    map '/subapp' do
        run SubApp::Application
    end
end

described in this question, and required:

config.action_controller.relative_url_root = '/subapp'

No other Rails configuration was necessary (e.g. scoping routes to '/subapp' on production). The nginx configuration as described above works.

Community
  • 1
  • 1
danebez
  • 353
  • 4
  • 15
0

You don't have to change config.ru (that basically removes the /subapp segment) if you do:

location /subapp/ {
  ...
  proxy_pass http://subapp/;
}

The (added) ending slash of the proxy_pass http://subapp/; line will remove the /subapp segment from the route received by rails.

user712557
  • 327
  • 2
  • 5