Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I answered this on a question I made myself here:</p> <p><a href="https://stackoverflow.com/questions/18134046/multiple-rails-4-app-using-nginx-unicorn/18161635#18161635">Multiple Rails 4 app using nginx + unicorn</a></p> <p>But here's a answer:</p> <p>I'm using <strong>Ruby 2.0</strong> and <strong>Rails 4.0</strong>. I suppose you already have nginx and unicorn installed. So, let's get started!</p> <p>In you <em>nginx.conf</em> file we are going to make nginx point to a unicorn socket:</p> <pre><code>upstream unicorn_socket_for_myapp { server unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0; } </code></pre> <p>Then, with your server listening to port 80, add a location block that points to the subdirectory your rails app is (this code, must be inside server block):</p> <pre><code>location /myapp/ { try_files $uri @unicorn_proxy; } location @unicorn_proxy { proxy_pass http://unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock; 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; } </code></pre> <p>Now you can just Unicorn as a Deamon:</p> <pre><code>sudo unicorn_rails -c config/unicorn.rb -D </code></pre> <p>The last thing to do, and the one I dug the most is to add a scope for your rails routes file, like this:</p> <pre><code>MyApp::Application.routes.draw do scope '/myapp' do root :to =&gt; 'welcome#home' # other routes are always inside this block # ... end end </code></pre> <p>This way, your app will map a link <em>/myapp/welcome</em>, intead of just <em>/welcome</em></p> <h2>But there's a even better way</h2> <p>Well, the above will work on production server, but what about development? Are you going to develop normally then on deployment you change your rails config? For every single app? That's not needed.</p> <p>So, you need to create a new module that we are going to put at <code>lib/route_scoper.rb</code>:</p> <pre><code>require 'rails/application' module RouteScoper def self.root Rails.application.config.root_directory rescue NameError '/' end end </code></pre> <p>After that, in your <code>routes.rb</code> do this:</p> <pre><code>require_relative '../lib/route_scoper' MyApp::Application.routes.draw do scope RouteScoper.root do root :to =&gt; 'welcome#home' # other routes are always inside this block # ... end end </code></pre> <p>What we are doing is to see if the root directory is specified, if so use it, otherwise, got to "/". Now we just need to point the root directory on config/enviroments/production.rb:</p> <pre><code>MyApp::Application.configure do # Contains configurations for the production environment # ... # Serve the application at /myapp config.root_directory = '/myapp' end </code></pre> <p>In config/enviroments/development.rb I do not specify the config.root_directory. This way it uses the normal url root.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload