404 Not Found error in deploying rails 3.2.12 app (with engines) to SUB URI on nginx/passenger
Asked Answered
A

2

4

We need to deploy a rails 3.2.12 app to sub uri nbhy on a ubuntu 12.04 server. The rails app has 3 engines and one of them is authentify which is for user authentication. The main app's root pointing to authentify's signin page. Here is the routes.rb in main app:

  root :to => "authentify::sessions#new"
  match '/signin',  :to => 'authentify::sessions#new'
  match '/signout', :to => 'authentify::sessions#destroy'
  match '/user_menus', :to => 'user_menus#index'
  match '/view_handler', :to => 'authentify::application#view_handler'

The app is deployed to base uri nbhy running on ubuntu 12.04 with passenger and nginx. On the same server, there is another rails app running in its own sub uri. Here is the configuration in nginx.conf for sub uri nbhy:

server {
   listen 80;
   server_name 6.95.225.93;
   root /var/www/;
   passenger_enabled on;
   rails_env production;
   passenger_base_uri /by;
   passenger_base_uri /nbhy;

   #for rails >=3.1, assets pipeline
   location ~ ^/assets/ {
     expires max;
     add_header Cache-Control public;
     add_header ETag "";
     break;
   }
}

Also a symlink nbhy is created at document root /var/www pointing to /var/www/nbhyop/current/public. Here is the output of the root /var/www/:

total 8
lrwxrwxrwx 1 cjadmin www-data   28 Nov  3  2012 by -> /var/www/byop/current/public
drwxrwsr-x 4 cjadmin www-data 4096 Nov  4  2012 byop
lrwxrwxrwx 1 cjadmin www-data   30 May 16 21:27 nbhy -> /var/www/nbhyop/current/public
drwxrwsr-x 4 cjadmin www-data 4096 May 14 15:21 nbhyop

The by is the first rails app deployed to the sub URI and is working fine.

The login page is displayed after typing http://6.95.225.93/nbhy. After key in user and password, the page was redirected to http://6.95.225.93/authentify/session with 404 Not Found error. There is an error found in nginx error.log:

2013/05/13 16:29:25 [error] 2384#0: *1 open() "/var/www/authentify/session" failed (2: No such file or directory), client: 192.168.1.1, server: 6.95.225.93, request: "POST /authentify/session HTTP/1.1", host: "6.95.225.93", referrer: "http://6.95.225.93/nbhy/"

Obviously /var/www/authentify/session will not hit the right page because it is missing the base uri nbhy between www and authentify. Based on our analysis, the create in authentify session controller hasn't been hit and the user hasn't been authenticated even with the right user name and password at http://6.95.225.93/nbhy.

Also find out that a user can login at http://6.95.225.93/nbhy/authentify/session/new with some twist. After login the page will be redirected to http://6.95.225.93/user_menus which will throw out 404 Not Found error. However if we insert nbhy in between as : http://6.95.225.93/nbhy/user_menus, then it will bring up the user menus page successfully. For any further click on links, manually inserting nbhy will make the link work (if nbhy is missing).

The rails app worked fine when deploying without sub uri.

Why the sub uri is missing from route? Is there a way we can make the nbhy here to stay and eliminate the error? Thanks for help.

Arthritis answered 13/5, 2013 at 22:28 Comment(0)
K
2

Most likely the authentify engine is doing a redirect to /user_menus, instead of /nbhy/authentify. Is this a custom Rails or Sinatra app that you have written? If so, you need to change/configure the code of authentify to always append the current subdirectory under which the Rails app is hosted. You can get that from passenger by saying ENV['RAILS_RELATIVE_URL_ROOT'] in your code.

Kiangsi answered 19/5, 2013 at 20:50 Comment(8)
redirect_to '/user_menus' . You are right. After a user is authorized, the authentify is redirect to /user_menus as above. Can we do: redirect_to ENV['RAILS_RELATIVE_URL_ROOT']+'/user_menus'? Is the line above OK in development?Arthritis
Also do we need to put this relative url root in every redirect in the app? We still don't quite understand why the last 2 deployment to sub uri are working fine (just with the config in nginx.conf and symlink like above) and this one with engines is getting so difficulty.Arthritis
Unfortunately yes, you need to use ENV['RAILS_RELATIVE_URL_ROOT'] + '/user/menus'. If you're using the Rails path helpers (like user_path, login_path, etc) then you have no problem, they will automatically append the relative URL root. But if you're doing your own redirect to a custom path, then you need to do this yourself.Kiangsi
I'd suggest creating a helper function like def redirect_relative_to, which appends the ENV['RAILS_RELATIVE_URL_ROOT'] automatically if the given URL starts with /. And then you could use this function everywhere instead of the normal redirect_to.Kiangsi
There is a constant SUBURI defined in the app which is equal to '/nbhy' for production and '' for development/test. SUBURI is prefixed to the url. In previous deployment, we notice that there is no need to prefix SUBURI to regular path such as users_path. SUBURI is only needed for custom route. Is this still true in this deployment? (we do not know whole lot about routing in rails).Arthritis
If changing to redirec_to user_menus_path, can we go by without prefixing SUBURI /nbhy? Our experience is no. But we don't quite understand why not as user_menus_path is a regular path. We thought if first redirect_to has the SUBURI prefixed, then this suburi will be carried along in following routing/redirect. Obviously this is not right and we don't quite understand why.Arthritis
After prefix with SUBURI to routes in main app, the links start working, except the login page (on 6.95.225.93/nbhy). Is there a way 6.95.225.93/nbhy pointing to nbhy/authentify/signin automatically so the login will not have error?Arthritis
When compiling assets, just do bundle exec rake assets:precompile RAILS_RELATIVE_URL_ROOT=/nbhyArthritis
T
1

Rails is generating paths straight from http://6.95.225.93 instead of http://6.95.225.93/nbhy.

You probably need to scope all your routes to "/nbhy".

config/routes.rb

scope "/nbhy" do
  ...
end
Transpire answered 17/5, 2013 at 13:13 Comment(3)
Will try scope again. Also do we need to keep current config in nginx and symlink?Arthritis
One more question about scope in routes.rb: How to handle the route in development for no nbhy? Can we do scope "/nbhy" || "/" do?Arthritis
With sope '/nbhy' do, the user menus page shows up with 6.95.225.93/nbhy/nbhy/user_menus after manually inserting one extra /nbhy. We can login at 6.95.225.93/nbhy/nbhy/authentify/session/new. It seems having the same routing behavior without scope.Arthritis

© 2022 - 2024 — McMap. All rights reserved.