Search code examples
ruby-on-rails-3nginxubuntu-12.04unicorn

Host multiple rails applications over subfolder using nginx+unicorn


I would like to host multiple rails applications using nginx + unicorn which is currently being served using apache + passenger with railsbaseuri. The only reason is being apache needs to be reloaded after every new application is deployed. I would like to know if adding new application is possible in unicorn+nginx without reloading server.

I want to deploy applications on subfolder like host-name/nginx-app1, host-name/nginx-app2 while host-name points to a basic html page.

Read somewhere related to using sockets to handle individual applications and would be looking for some help to implement this. In my case the application is deployed only once with no further iterations. Once i deploy the new application, there should be no downtime in order to make the current application running.

EDIT config/unicorn.rb file inside the application.

    working_directory "/home/ubuntu/application_1"
    pid "/home/ubuntu/application_1/pids/unicorn.pid"
    stderr_path "/home/ubuntu/application_1/log/unicorn.log"
    stdout_path "/home/ubuntu/application_1/log/unicorn.log"
    listen "/tmp/unicorn.todo.sock"
    worker_processes 2
    timeout 30

Solution

  • One way to go about it is hosting the rails applications as UDS. And nginx to have multiple server blocks to read from each UDS (Unix Domain Sockets). Writing the logic adhoc pardon for syntax errors.

    e.g. Have a look at this.
    http://projects.puppetlabs.com/projects/1/wiki/using_unicorn

    You can host app1 using app1.conf for unicorn which will have a line.

    listen '/var/run/app1.sock', :backlog => 512
    

    and have multiple nginx upstreams like

    upstream app1 {
            server unix:/var/run/app1.sock fail_timeout=0;
        }
    upstream app2 {
            server unix:/var/run/app2.sock fail_timeout=0;
        }
      ....
    

    and route requests (proxypass) from a server block based on location or host header

     server {
            listen 80;    
            location /app1 { 
                proxy_pass http://app1;
                proxy_redirect off; 
            } 
            location /app2 { 
                proxy_pass http://app2;
                proxy_redirect off; 
            } 
        }