damon
By:
damon

Nginx and Unicorn Multiple Rails apps

June 14, 2014 12k views
Hello, I followed this guide and everything went smooth! https://www.digitalocean.com/community/tutorials/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5 I got a rails app up and running with no problems. But I have spent a lot of time tiring to get multiple apps running with no success. I have tried to edit the TheApp/config/unicorn.rb for each app and /etc/nginx/conf.d/default.conf. Are there any examples of how these files should be setup for multiple apps? Also: When I run unicorn_rails -c config/unicorn.rb -D for the 2nd app i get the following error: /usr/local/rvm/gems/ruby-2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:75:in `read': No such file or directory @ rb_sysopen - config/unicorn.rb (Errno::ENOENT) Can anyone help please?
1 comment
2 Answers
For each app, you need to make sure unicorn is listening on a different socket. In the app's config/unicorn.rb, change the socket value to be unique:
listen "/tmp/unicorn.app_one.sock"
Then in the Nginx configuration, you'll need a new server block for each app. It will look something like:
upstream app_one {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.app_one.sock fail_timeout=0;
}

upstream app_two {
    server unix:/tmp/unicorn.app_two.sock fail_timeout=0;
}

server {
    listen 80;
    server_name domain-for-app_one.com;

    # Application root, as defined previously
    root /root/app_one/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_one;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
} 

server {
    listen 80;
    server_name domain-for-app_two.com;

    # Application root, as defined previously
    root /root/app_two/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_two;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}  
Have your tried VirtualHosts? With properly setup you can have different domains/folder.
Have another answer? Share your knowledge.