Nginx – Pass on passenger_base_uri to Rails app

debiannginxphusion-passengerruby-on-rails

We built a Rails app that consists of the following routes:

namespace :api do
    namespace :v1 do                  
      resources ...
    end
end

That means, all controllers are reachable via www.url.com/api/v1/controller. To run a stand-alone web app under the root domain, I have the following local nginx setup:

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   /Users/swramba/Sites;
        index  index.html index.htm;
    }

    location ~ /api {
        proxy_pass http://127.0.0.1:3000;
    }
}

This means, everything beneath /api gets proxied to the WEBrick server used for development. Now, in our production environment, we wanted to use Phusion Passenger with a similar setup. So, I wrote the following config for the production nginx:

server {
    listen       80;
    server_name  localhost;

    root /home/someuser/sites/web/www.myapp.com;

    passenger_base_uri /api;
    passenger_enabled on;
    rails_env production;
}

But, this means that the /api part is stripped from the URL and Rails returns 404, since /v1/something cannot be found.

How can I make nginx or Passenger not omit the /api from the URL, so that the URL is the same as in development?

I also tried to set config.relative_url_root = "/api" but this did not have any effect.

Best Answer

I could figure it out. This is actually the right configuration:

server {
    listen       80;
    server_name  localhost;

    location / {
        root path/to/some_html_webapp;
    }

    location ~ /api {
        root path/to/rails_app/public;
        passenger_enabled on;
        rails_env production;
    }

}

This way, the URL is not modified, but Passenger takes over at a certain path.

Related Topic