multiple rails apps on nginx and unicorn

后端 未结 3 915
北荒
北荒 2020-12-07 23:32

I successfully setup a rails site using the Screencast 335 deploy to a VPS tutorial. Now I want to add another rails app on a new domain but I am confused about the steps re

3条回答
  •  青春惊慌失措
    2020-12-08 00:22

    I answered this on a question I made myself here:

    Multiple Rails 4 app using nginx + unicorn

    But here's a answer:

    I'm using Ruby 2.0 and Rails 4.0. I suppose you already have nginx and unicorn installed. So, let's get started!

    In you nginx.conf file we are going to make nginx point to a unicorn socket:

    upstream unicorn_socket_for_myapp {
      server unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0;
    }
    

    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):

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

    Now you can just Unicorn as a Deamon:

    sudo unicorn_rails -c config/unicorn.rb -D
    

    The last thing to do, and the one I dug the most is to add a scope for your rails routes file, like this:

    MyApp::Application.routes.draw do
      scope '/myapp' do
        root :to => 'welcome#home'
    
        # other routes are always inside this block
        # ...
      end
    end
    

    This way, your app will map a link /myapp/welcome, intead of just /welcome

    But there's a even better way

    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.

    So, you need to create a new module that we are going to put at lib/route_scoper.rb:

    require 'rails/application'
    
    module RouteScoper
      def self.root
        Rails.application.config.root_directory
      rescue NameError
        '/'
      end
    end
    

    After that, in your routes.rb do this:

    require_relative '../lib/route_scoper'
    
    MyApp::Application.routes.draw do
      scope RouteScoper.root do
        root :to => 'welcome#home'
    
        # other routes are always inside this block
        # ...
      end
    end
    

    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:

    MyApp::Application.configure do
      # Contains configurations for the production environment
      # ...
    
      # Serve the application at /myapp
      config.root_directory = '/myapp'
    end
    

    In config/enviroments/development.rb I do not specify the config.root_directory. This way it uses the normal url root.

提交回复
热议问题