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

I am trying to use the instructions at to setup the sub uri ( 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:/ 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?

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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


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: