How to make unicorn run a Rails 3.0 app under a path?

余生长醉 提交于 2019-12-11 01:55:44

问题


I'm migrating from Passenger to Unicorn, and with Passenger I used to run my Rails 3.0 app with the RailsBaseURI option to prefix all URLs with '/blah' for example.

When running the same app under Unicorn, I pass '--path /blah' to unicorn_rails, but the server still serves things as http://server:3000/etc/etc, instead of http://server:3000/blah/etc/etc - I see some assets being requested via /blah/ (like JS files, and so on), but the various links still do not have '/blah/' before them.

So - to summarise - how to I get Unicorn to mount my app under /blah, so that all requests work with '/blah' prepended to the path and all links and assets are created with '/blah/' prepended to them?

Thanks


回答1:


Here's what I've discovered:

To get a Rails app to serve asset and link URLs, you have to set an environment variable before it starts:

ENV['RAILS_RELATIVE_URL_ROOT'] = '/prefix'

Then, as @d11wtq said, to get the Rack-compatible server (in this case, Unicorn) to actually serve the app under a prefix, you have to, in your config.ru file, instead of the usual run line, do the following:

run Rack::URLMap.new(
  ENV['RAILS_RELATIVE_URL_ROOT'] => Rails.application
)



回答2:


I believe putting something like this in your config.ru should do it:

require ::File.expand_path('../config/environment',  __FILE__)

run Rack::URLMap.new(
  "/blah" => Rails.application
)

untested




回答3:


The easy and reliable solution is to set the prefix in your front-end setup.

With passenger+nginx for example, you could use something like this:

server {
    listen 80;
    server_name www.phusion.nl;
    root /websites/phusion;
    passenger_enabled on;
    passenger_base_uri /prefix;
}

Nothing to change in the source code, one line setup.


But you said you're using unicorn, which front-end are you using ?

Using nginx is should be possible with something like this:

location /prefix {
    rewrite           ^/prefix/(.*)  /$1      break;
    proxy_pass        http://localhost:8000;
}


来源:https://stackoverflow.com/questions/10386305/how-to-make-unicorn-run-a-rails-3-0-app-under-a-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!