Running Rails+Passenger+Devise from a subdirectory? - ruby-on-rails

I have a server A which proxies all traffic on /rails to server B.
So I setup this virtual host, and most things work...okay. link_to is broken and generates urls to /users as opposed to /rails/users, but I can work around that.
If I set config.action_controller.relative_url_root to /rails then my routes work okay EXCEPT all the devise routes. They point to the bare URL. How do I properly configure server B to understand that its running in a subdirectory and generate links and routes correctly?
<VirtualHost *:80>
ServerName http://ec2-url.compute-1.amazonaws.com/
SetEnv RDS_HOSTNAME "mydb..."
SetEnv RAILS_RELATIVE_URL_ROOT "/rails"
DocumentRoot /home/ubuntu/myapp/public
RailsEnv staging
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/app.log combined
PassengerLogLevel 3
<Directory "/home/ubuntu/myapp/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
I am using Rails 4.

In your environment files, add a config for OmniAuth.config.full_host.
OmniAuth.config.full_host = 'http://myfullurl/subdir'
Now, in application_controller.rb, add this method:
def after_sign_in_path_for(resource_or_scope)
path = super(resource_or_scope)
"#{OmniAuth.config.full_host}#{path}"
end

Would be great if you share your routes.rb, but I think the easy way to change devise routes is to put something like the following in routes.rb
scope '/rails' do
devise_for :users
end

Related

How to create dynamic subdomain rails

How can i create subdomain for each user that registers on my site? For example userone.mysite.com and usertwo.mysite.com.
In php it can be done using apache virtual host, but i can't figure out how to do the same in Ruby on Rails. Here is how it can be done in apache
<VirtualHost *:80>
ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com
DocumentRoot /www/domain
</VirtualHost>
I went through many blogs but couldn't find the solution.
Please Advice.
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.my_site.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/html/my_site
<Directory /var/www/html/my_site>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
# Uncomment this if you're on Apache >= 2.4:
Require all granted
</Directory>
</VirtualHost>
and do not forgot change cname * entry to your domain in DNS
Addition to Dinesh Saini answer - you also need to update Rails configs accordingly. For example, if you need deep subdomains you should change
config.action_dispatch.tld_length in staging.rb and you should re-check routes.rb.
Live example: I had to implement shop displaying by subdomain - example URL
my-shop.shop.testapp.com.So what I did except server config changes
constraints (lambda { |req| req.subdomains[1] == 'shop' }) do
get '/', to: 'shopes#show', as: :shop
end
In controller to find resource
Shop.find_by(id: request.subdomains[0])
I also set
config.action_dispatch.tld_length = 2
I did this for staging env because it has such URL staging.testapp.com so I need another subdomain level. I think for you it's only good to check that it's has 1.

Rails 3 + passenger conf file

I have been trying to install a rails app on a box that originally was used for multiple php applications. I installed passenger and created a conf file but I am confused about how it all works together.
What should my ServerName be if i want to access a Rails app? The box is a debian squeeze running apache2 with passenger module installed. Below is my conf file. I currently have to go to xxx.xxx.xxx.xxx/leoadmin/public to get the application to run properly. I have the following htaccess in the public directory. Which I think is unnecessary if my conf file is configured correctly.
I think my main issue is the misconfiguration of the conf file. I believe the conf file is being loaded but i don't understand why i still need the htaccess for me to see any action.
.htaccess
#PassengerEnabled On
PassengerAppRoot /var/www/leoadmin/
#Options -MultiViews
#PassengerResolveSymlinksInDocumentRoot on
#Set this to whatever environment you'll be running in
RailsEnv production
#RackBaseURI /var/www/leoadmin
vhost config
<VirtualHost *>
ServerName leoadmin
DocumentRoot /var/www/leoadmin/public
<Directory /var/www/leoadmin/public>
Allow from all
</Directory>
Alias /leoadmin /var/www/leoadmin/public
<Location /leoadmin>
SetEnv RAILS_RELATIVE_URL_ROOT "/leoadmin"
PassengerAppRoot /var/www/leoadmin
</Location>
ErrorLog /var/log/apache2/leoadmin-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/leoadmin-access.log combined
</VirtualHost>
I had nothing but pain and more pain trying to get this sorted a few months back, my configuration allows me to do sub URIs, so I'm not sure if that's what you want, but it'll be along these lines. I've never needed a .htaccess file for this setup:
NameVirtualHost *:80 <VirtualHost *:80>
ServerName www.test.co.uk
DocumentRoot /web/rails
<Directory /web/rails>
Allow from all
</Directory>
RailsBaseURI /test
RailsEnv development
<Directory /web/rails_projects/test/>
Options -MultiViews
</Directory>
For me /web/rails is a root directory, from there I have a number of symlinks to my /home/ directory where I store my projects. ServerName is whatever qualified name you're using for the machine that's going to be recognised.
(just in case, I'm running Passenger 3.0.9, and Rails 3.2.11)

RackEnv not respected when using route helpers from model

Running Rails 3.2.13 on Passenger for Apache. My vhost:
<VirtualHost *:80>
ServerName server
DocumentRoot /srv/http
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
RackEnv test
RackBaseURI /rails_app
<Directory /srv/http/rails_app>
Options -MultiViews
</Directory>
</VirtualHost>
Works great. Only problem is that I'm using the route url helpers in a model, like so:
# egg.rb
def to_exhibit
return {
:edit_path => Rails.application.routes.url_helpers.edit_egg_path(self)
}
end
When rendering URLs in views, the sub-URI is used as I'd expect it to be, but when accessing the URL helper from within a model, it's discarded and the paths are always relative to the root.
From eggs_controller.rb:
edit_egg_path(1000) --> /rails_app/eggs/1000/edit
From some_model.rb:
edit_egg_path(1000) --> /eggs/1000/edit
Should this work as I expect it to? I don't mind manually fixing this at all, but I'm stumped as to where to find the value of the RackBaseURI so I can insert it manually, if it exists. I'd prefer not to manually define it again in some environment conf since Rails must know about it already.

Passenger on bitnami - can't access app root, all other routes working

I created a bitnami instance on AWS, and uploaded my rails application at -
/home/bitnami/my_app
I added the following to my virtual hosts -
<VirtualHost *:80>
ServerAdmin test#test.com
DocumentRoot "/home/bitnami/my_app/public"
<Directory /home/bitnami/my_app/public>
Allow from all
Options -MultiViews
</Directory>
RailsEnv production
ServerName www.my_app.com
ErrorLog "logs/my_app.com-error_log"
CustomLog "logs/my_app.com-access_log" common
</VirtualHost>
Inside the application routes.rb, I have -
root :to => "pages#home"
I can access all the routes in the application, i.e. if I go to www.my_app.com/signin, it works.
However, trying to access root - i.e. www.my_app.com gives 404 error.
What am I missing? Thanks in advance for your help.
Have you enabled Passenger in the Apache configuration? The guide below may help:
http://wiki.bitnami.com/Components/Phusion_Passenger

Passenger: RailsBaseURI case sensitive?

I used Passenger to deploy a RoR app to a sub URI on my domain. The problem I'm facing is that the sub URI seems to be case sensitive. Navigating to http://mydomain.com/RailsApp resolves fine. However, if I go to http://mydomain.com/railsapp, http://mydomain.com/railsApp, or any other variation, I get a 404 error. How can these requests using different casings get resolved correctly?
Here is my Apache configuration file:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /www/mydomain/public
<Directory "/www/mydomain/public">
RailsEnv "production"
Order allow,deny
Allow from all
</Directory>
RailsBaseURI /RailsApp
<Directory "/www/RailsApp/public">
RailsEnv "development"
Options -MultiViews
</Directory>
</VirtualHost>
Any help is much appreciated. Thanks!
You could look into using mod_rewrite and matching it case insensitive there.
Some links to get you started :)
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.2/misc/rewriteguide.html
Thanks ba for pointing me in the right direction.
I did some research and found the mod_speling module. This not only makes the URL case-insensitive but also checks for spelling errors.
To enable mod_speling:
sudo /usr/sbin/a2enmod speling
sudo /etc/init.d/apache2 force-reload
sudo /etc/init.d/apache2 restart
To use mod_speling, include the directive CheckSpelling on in your virtual host section:
<VirtualHost *:80>
CheckSpelling on
...
</VirtualHost>

Resources