Skip to content

Instantly share code, notes, and snippets.

@coffeencoke
Last active May 19, 2017 08:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coffeencoke/4422617 to your computer and use it in GitHub Desktop.
Save coffeencoke/4422617 to your computer and use it in GitHub Desktop.
Use a sub directory as a rails application's url root rails 3, nginx, unicorn

Symlink for static files

Among applying the changes in the other files in this gist, you need to symlink the directory you are serving the app with in the root app's public directory so that NGINX can serve the static files in the sub-app.

For example, if you have an app at /home/coffeencoke/apps/app.coffeencoke.com/current/public as your root app, and you have the sub app served at http://app.coffeencoke.com/admin, then you need to symlink /home/coffeencoke/apps/admin-app.coffeencoke.com/current/public to /home/coffeencoke/apps/app.coffeencoke.com/current/public/admin like so:

ln -s /home/coffeencoke/apps/admin-app.coffeencoke.com/current/public /home/coffeencoke/apps/app.coffeencoke.com/current/public/admin

Also...

Issue when deploying a fresh environment and running deploy:cold you may get an error that says "cannot stat /....manifest.yml" Added an issue in github: capistrano/capistrano#379 Work around is to manually run the rake assets precompile method in app/shared/cached-copy and move the assets/admin/manifest.yml file to /app/shared/assets/manifest.yml

$ cap ui deploy:setup deploy:cold # this will fail, but will setup our files for us
$ ssh coffeencoke@app.coffeencoke.com
$ cd ~/apps/admin-app.coffeencoke.com/shared/cached-copy
$ bundle install --without test development
$ bundle exec rake RAILS_ENV=qa RAILS_GROUPS=assets assets:precompile
$ cp -- public/admin/assets/manifest.yml ../assets/
$ exit
$ cap ui deploy:cold
# This is for the admin app only
#
# Among other app config things, add these two lines within your Application.configure block
#
# You can also put this in the config file for each individual environment. So you can still use
# localhost:3000 w/out the subdirectory for development mode and tests and such.
#
AppAdmin::Application.configure do
# ...
# Serve the app from host/admin
config.root_directory = '/admin'
config.assets.prefix = '/admin/assets/'
end
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name app.coffeencoke.com app;
access_log /var/log/nginx/app.coffeencoke.com.access.log;
error_log /var/log/nginx/app.coffeencoke.com.error.log info;
root /home/coffeencoke/app.coffeencoke.com/public;
location / {
try_files $uri @unicorn_proxy;
}
location @unicorn_proxy {
proxy_pass http://127.0.0.1:8081;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /admin {
try_files $uri @unicorn_proxy_admin;
}
location @unicorn_proxy_admin {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
require 'rails/application'
module RouteScoper
# Keep the rescue so that you can revert to not having a
# subdirectory when in development and test modes
def self.root
Rails.application.config.root_directory
rescue NameError
'/'
end
end
MagnetTesterAdmin::Application.routes.draw do
scope RouteScoper.root do
resource :session, only: :new
root :to => 'awesome_rootness#show'
end
end
@nezirz
Copy link

nezirz commented Aug 5, 2015

is it possible to make short video tutorial about this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment