user6175355
user6175355

Reputation:

rails subdomain on localhost

I would like to test subdomains on localhost. I have following instructions: Before starting the server to run the application, the host should be configured so that the application can support subdomains for multiple users. To do that, go to your hosts file by typing sudo nano /etc/hosts in your terminal, then add subdomains at the end of the file as below:

127.0.0.1 admin.daycare.no
127.0.0.1 daycare.no
127.0.0.1 worker.daycare.no
127.0.0.1 manager.daycare.no
127.0.0.1 parent.daycare.no

I followed the above instructions. The following error was encountered while trying to retrieve the URL: http://daycare.no:3000/

Unable to determine IP address from hostname daycare.no
The DNS server returned: Name Error: The domain name does not exist.
This means that the cache was not able to resolve the hostname presented in the URL. 
Check if the address is correct.

How can I solve this, please?

Upvotes: 13

Views: 11204

Answers (4)

TheVinspro
TheVinspro

Reputation: 370

I using rails 6. You need to just follow 3 steps only and you can access your rails server with your defined URL without mentioning the port number.

  1. Edit your hosts file. (as you have done already).

    nano /etc/hosts
    

add your URLs.

   127.0.0.1 admin.daycare.no
   127.0.0.1 daycare.no
   127.0.0.1 worker.daycare.no
   127.0.0.1 manager.daycare.no
   127.0.0.1 parent.daycare.no 
  1. Add all URLs to the environment configuration

    config.hosts << 'admin.daycare.no'
    config.hosts << 'daycare.no'
    config.hosts << 'worker.daycare.no'
    config.hosts << 'manager.daycare.no'
    config.hosts << 'parent.daycare.no'
    
  2. Now just run

     rvmsudo rails s -p80
    

Open your url http://admin.daycare.no on browser.

Upvotes: 2

demir
demir

Reputation: 4709

I use www.vcap.me:3000

There is no need to do extra configuration before rails 6.

with rails 6, you need to add www.vcap.me to environment configuration:

config.hosts << 'www.vcap.me'

You can use it like this:

www.vcap.me:3000
subdomain1.vcap.me:3000
subdomain2.vcap.me:3000
..

Upvotes: 4

7urkm3n
7urkm3n

Reputation: 6311

After saving /etc/hosts file, run your rails app like this

rails s -p 3000 -b daycare.no

In a browser

 http://daycare.no:3000

Personally, I use lvh.me:3000 just running

rails s -p 3000 -b lvh.me

no need to touch /etc/hosts file.

By default, you can't browse the link lvh.me:3000 without internet connection, solution is to add 127.0.0.1 lvh.me into host file.

# /etc/hosts file
127.0.0.1   localhost
127.0.0.1   lvh.me #make sure lvh.me is after localhost otherwise will not work

But, Its little annoying to run this each time when restarting a server.

Set your custom command:

sudo nano .bash_profile
# OR
sudo nano .bashrc
# OR
sudo nano .profile

And ADD these lines to there:

alias lvh='rails s -p 3000 -b lvh.me'
alias lvh_production='RAILS_ENV=production rails s -p 3000 -b lvh.me' #production

Do not forget restart your terminal tab, close and open new tab OR run this command on the same tab . ~/.bash_profile depends what you have used on top.


Alternate solution is POW (LINK) server can give you custom domain smth like daycare.dev.

Upvotes: 18

tadman
tadman

Reputation: 211560

Editing your hosts file is an extremely messy way of doing this sort of thing. The problem here is some software doesn't read that file and goes directly to DNS to do resolving.

A service like xip.io can help. You can use addresses like:

http://test.127.0.0.1.xip.io/

That will always resolve to 127.0.0.1.

There's also things like Puma dev which come with their own resolver for the .test domain so site.test works locally without having to edit any files.

The advantage of both systems is you can add on arbitrary bits and it still works: subdomain.test.test and subdomain.127.0.0.1.xip.io also resolves the same way.

Upvotes: 0

Related Questions