navjeetc
By:
navjeetc

How to setup sub uri based Rails apps with Nginx and Unicorn?

March 27, 2015 1.3k views
Ruby on Rails Nginx Ubuntu

I am trying to use the instructions at http://jrochelly.com/post/2013/08/nginx-unicorn-multiple-rails-apps/ to setup the sub uri (www.mydomain.com/site1 www.mydomain.com/site2) based Rails applications using Nginx and Unicorn.

I started with Droplet that had Unicorn and Nginx pre-installed from DO and it works fine. Now I want to setup multiple sites/Rails apps based on Sub uri.

As per the instructions I have updated nginx.conf by adding follwoing to current nginx.conf

upstream unicorn_socket_for_site2 {
  server unix:/home/navjeetc/site2/current/tmp/sockets/unicorn.sock fail_timeout=0;
}

Then I created a file site2 under sites-enabled as below:

server {
  location /site2/ {
    try_files $uri @unicorn_proxy;
  }

  location @unicorn_proxy {
    proxy_pass http://unix:/home/navjeetc/site2/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;
  }

}

And then made changes to my site2 Rails app as suggested. I did not change unicorn conf file.

But the request to http:/subdomain.mydomain.com/site2 is still going to the old Rails app and I get a 404.

The nginx config file for the old Rails app is:

server {
    listen 80;
    root /home/rails/current/public;
    server_name _;
    index index.htm index.html;

    location / {
        try_files $uri/index.html $uri.html $uri @app;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
    try_files $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_server;
  }

}

How can I fix this?

1 Answer

You'd need to create a separate Unicorn configuration for each app. The Nginx setup that you've shared above expects Unicorn to server the Rails app locally on a Unix socket found at /home/navjeetc/site2/current/tmp/sockets/unicorn.sock Which is fine, but Unicorn isn't actually running the second app.

If you look at /home/unicorn/unicorn.conf you'll see that the first app is being served at 127.0.0.1:8080 which is then proxied by Nginx. The Unicorn process is started using an initscript located in /etc/init.d/unicorn You'd need a second Unicorn process to run the second app.

In total, there are three files which you would need to copy and adjust:

/home/unicorn/unicorn.conf
/etc/default/unicorn
/etc/init.d/unicorn

In many ways, it might be simpler to start from scratch than trying to repurpose the One-Click image which was build with only one app in mind. Check out this tutorial for more information. You should be able to simply follow it duplicating each step for the second app:

When you are ready to deploy your Ruby on Rails application, there are many valid setups to consider. This tutorial will help you deploy the production environment of your Ruby on Rails application, with PostgreSQL as the database, using Unicorn and Nginx on Ubuntu...
Have another answer? Share your knowledge.