Report this

What is the reason for this report?

How to redirect subdomain to another rails project

Posted on May 7, 2016

I have a project on home/rails/rails_project (myapp.com) I’v already created the subdmain: test.myapp.com

I want that test.myapp.com redirect to home/rails/project2 How can I do that? thanks



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!

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.

The easiest way to serve two separate Rails apps from the same Droplet is to use an Nginx reverse proxy. If you’re not already doing so, take a look at this tutorial:

Bellow is a sample Nginx configuration based on the one used by the DigitalOcean Rails One-Click app. You’ll see a few key things. First, it is important to set the server_name directives so that Nginx can correctly route requests to the apps based on their domain names.

Next you’ll see that we created a new upstream server. The Unix socket file that it points to is defined in listen directive in the Unicorn configuration for your project.

upstream app_server {
    server unix:/var/run/unicorn.sock fail_timeout=0;
}

upstream project2_server {
    server unix:/var/run/project2.sock fail_timeout=0;
}

server {
    listen   80;
    root /home/rails/rails_project/public;
    server_name myapp.com;
    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;
    }
}

server {
    listen   80;
    root /home/rails/project2/public;
    server_name test.myapp.com ;
    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://project2_server;
    }
}

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.