How to transfer my domain to my rails application?

May 14, 2016 666 views
Deployment Ruby on Rails DNS Nginx

I am trying to transfer my domain. I am running my rails app using this command from my rails folder
RAILS_ENV=production rails s --binding=128.199.95.219
However, I can only access it through http://128.199.95.219:3000/ but not http://128.199.95.219/.

I might be wrong but I think I need to somehow serve my app from the root domain address like http://128.199.95.219/ not http://128.199.95.219:3000/. I have already point my domain name to DigitalOcean by following this tutorial And I have also setup the host name with DigitalOcean. So right now I just need to configure rails, nginx, and unicorn somehow to serve my app at the domain address or http://128.199.95.219/ (I don't even know if that is what I need to do. so please help me!)

I am running Ubuntu 14.04 with rails, nginx, and unicorn. I've configured my nginx and unicorn according to this tutorial here

How do I make rails application accessible from the root of the domain address (http://128.199.95.219/)? or is that eve necessary at all?

How exactly do I deploy my rails application? I am lost! Please help!

1 Answer

You need to use nginx to proxy HTTP requests to whatever port unicorn is running on. In this specific case that is port 3000.

When you go to any website using a web browser by default http://website.com is actually just sending a request to the IP address at website.com on port 80. Likewise, when you go to https://website.com it is sending a request to that IP address at website.com on port 443.

So, in your case you have unicorn running on port 3000, and you need to get nginx to listen on port 80 and proxy all requests to port 3000.

  1. SSH into your server
  2. Edit the nginx config file nano /etc/nginx/sites-available/default
  3. Replace everything in there with this:

    server {
    listen 80;
    
    location / {
            proxy_pass http://127.0.0.1:3000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }
    
  4. Restart nginx service nginx restart

You should now be able to access your rails app at both 128.199.95.219 and assuming you have an A record for YOUR_WEBSITE.COM pointed to 128.199.95.219 then you should also be able to access the app at http://YOUR_WEBSITE.COM

Hope this helps!

Have another answer? Share your knowledge.