Rails 4 in a subdirectory - ruby-on-rails

I have a dream, that one day, my Rails 4 app can be served from /sub in production.
Requests for /sub and /sub/projects would simply look like / and /projects to the app. All assets would be served from /sub/assets.
But still, in development environment, everything can be run from the root of localhost:3000.
Regardless of the server behind (apache / nginx), how do I need to configure the rails app?

If I understand you correct:
You can place project on production server everywhere you want. You just need describe it in apache or nginx config file. For apache use DocumentRoot directive. For nginx use server -> root syntax.

Related

How to change the configuration file to deploy a rails app with apache? on a subdomain

I was given an url http://XXXXXX.XXXX.XXX/avrccalendar/
and was asked to deploy my rails app on it.
At the beginning, there was a static webpage index.html in the /www directory, which is nothing but a message and was deleted by me.
This is my first time to deploy a rails app and I set my .conf like this:
VirtualHost *:80>
ServerName XXXXXX.XXXX.XXX/avrccalendar
DocumentRoot /home/avrccalendar/calendar/public
RailsEnv production
/VirtualHost>
I set it up that way, because I think rails app creates dynamic content, thus I didn't put the app files in the /www directory. However, when I visit the website it still get the static webpage index.html.
I cannot find whers the configuration is that set the url to return that static webpage.
And i would like to know how I should set .conf to make it to visite my rails app.
I have already installed passenger. The environment is Ubuntu 14.04.
Any advice would be helpful. Thank you~!

How to serve an api subdomain using apache, passenger, and rails?

I created a subdomain like the following myapp.example.com and it works great ...has been. Now I want to build an API using rails for the app (rails itself) so I created the subdomain api.myapp.example.com. Currently the problem I'm getting is the api subdomain isn't getting routed to the rails app.
I tried adding the following to the apache conf file in /etc/apache2/sites-enabled/example.com.conf
# example.com.conf
ServerAlias *.myapp.example.com
I then restarted Apache, but still api.myapp.example.com wasn't being routed to rails application.
Ahhh, it ended being an easy fix
# example.com.conf
ServerAlias myapp.example.com api.myapp.example.com

Trying to set up a simple nginx ruby on rails development. How do I run the app?

I am really struggling to understand this.
I have used PHP / Apache all my life but I just need to have a very basic setup of a Ruby on Rails app on an Nginx server for development.
I have set up an Ubuntu 12.04 server with Vagrant / VirtualBox.
I can see in the web browser the "Welcome to nginx!" message and there's a Ruby on Rails app installed on the server (tested with someone else and working on there server).
The Ruby on Rails application is in an /app/ folder and with these folders:
- assets
- controllers
- helpers
- mailers
- models
- observers
- uploaders
- views
Now, all I need to do is run the thing. Where is my index file?
Are there any other folders I should take note of? There's nothing I can see.
I've tried configuring sites-enabled but none of it makes sense. Surely it should be a simple case of getting my-domain.com to display the index file. Simple, surely? Which file in the /app/ folder am I trying to run?
For development you can use the built-in web-server with rails s in your project folder. It will runs your RoR app on 0.0.0.0:3000. Port can be changed with -p (rails s -p 80).
Read this guide to running rails on nginx using passenger:
Once Ruby on Rails is installed, go ahead and install passenger.
gem install passenger
...
Set the root to the public directory of your new rails project. Your
config should then look something like this:
server {
listen 80;
server_name example.com;
passenger_enabled on;
root /var/www/my_awesome_rails_app/public;
}
(Don't forget the /public after the rails root location!)

How to move redmine to a sub-URI with nginx as a proxy

I want to put redmine (listening on localhost:3000 standalone passenger) behind nginx, serving from http://domain.com/redmine with this simple nginx config:
server {
location /redmine/ {
proxy_pass http://domain.com:3000/;
}
}
I rewrote redmine's config/environments.rb based on the instructions from this HOWTO, and redmine properly rewrites static asset URLs, but controller URLS are not rewritten, for ex. settings point to http://domain.com/settings instead of http://domain.com/redmine/settings.
How can I properly configure redmine to run behind nginx?
This is a pretty complicated thing to do. You wouldn't think so but it is. Every single redmine version that comes out has a different set of correct and incorrect ways of accomplishing this. For example, I have a working configuration with (nginx-1.4.1, unicorn, redmine-2.2.1) but now that I'm using (nginx-1.4.2, unicorn, redmine-2.3.3) that doesn't seem to work anymore. Here's some links that may be of use:
http://www.redmine.org/issues/11881
http://www.redmine.org/issues/12102

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

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
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
)
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
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;
}

Resources