Rails: replace localhost:3000 with custom domain - ruby-on-rails

In development, I would like to replace localhost:3000 to something like "domain.com:3000" or "domain.com". I can achieve this by adding alias to /etc/hosts file, for ex:
# /etc/hosts
127.0.0.1 domain.com
^^^ this one works, i can now view my site locally using domain.com.
The problem is that rails itself still uses localhost:3000, for example when generating urls via router methods. I feel that there should be some config for this.

You have to set:
Rails.application.routes.default_url_options[:host] = 'domain.com:3000'
in your development environment.

Custom domain name for your IP Address can be created.
First of all find your IP Address using ifconfig.
Then open /etc/hosts file with sudo.
/etc/hosts file is in readonly, so need to open with sudo
sudo vi /etc/hosts
There you will find at least two entries for 127.0.0.1 and 127.0.1.1, below this, create your own domain name
<IP_Address> domain.com
Save the file.
Then start your rails server and bind with your IP Address.
rails s -b <IP_Address>
And you are done.
In browser,
<IP_Address>:3000 will be same as domain.com:3000

Related

Rails Server Route Error When Accessing Via IP Address

I am new here and working on a Rails application that was handed to me for a research project.
I have created my own instance of the project and I am able to access the server running the instance from my computer, BUT, only if I modify my hosts file and map the IP address to an arbitrary domain name.
If I try to access the rails server via an IP address, I get the following error:
"Routing Error
No route matches [GET] "/"
Try running rake routes for more information on available routes."
So my question is, how would I configure the Rails server and application to be accessible via IP address only, for example: http://52.78.233.65:3000/, and why would the server work if I modify the hosts file but not visit the site directly?
If you need remote access, you can choose one of below methods:
Apply in each start
Start rails server by appending -b 0.0.0.0, so command to start rails server is: rails s -b 52.78.233.65 (from your sample IP)
Change default binding option
Modify config/boot.rb by adding below code and start rails server with normal command rails s
config/boot.rb
require 'rubygems'
require 'rails/commands/server'
module Rails
class Server
alias :default_options_bk :default_options
def default_options
default_options_bk.merge!(Host: '0.0.0.0')
end
end
end

Rails - Configuring DNS to host with Heroku (123-reg)

I am trying to figure out how to publish my app on heroku. My DNS is with 123-reg. 123-reg does not provide any support for heroku deployment and vice versa.
I have previously asked this question (voted down and mocked in the comments)
Rails, Heroku - configuring 123-reg domain for heroku
I'm asking again in the hope of finding some substantive help on how to get this working.
Currently, when I try to render the page using my custom domain name, I get an error that says:
This site can’t be reached
www.mydomainname.com ’s server DNS address could not be found.
Try running Network Diagnostics.
DNS_PROBE_FINISHED_NXDOMAIN
When I try the following in the terminal, I get the responses noted below:
host www.mycustomdomainname.com
Host www.mycustomdomainname.com not found: 3(NXDOMAIN)
host https://www.mycustomdomainname.com
Host https://www.mycustomdomainname.com not found: 2(SERVFAIL)
dig www.mycustomdomainname.com cname +short
myherokualias.herokudns.com
heroku domains returns the heroku alias both with and without the www prefix.
In my DNS, I have a CNAME record with:
DNS ENTRY TYPE PRIORITY TTL DESTINATION/TARGET
www CNAME mycustomdomainname.herokudns.com.
Apart from that I have MX and TXT records for my email account.
Can anyone see what I need to do to get the page to render using my custom domain name?
When I try:
curl -vI https://www.mycustomdomainname.com
* Rebuilt URL to: https://www.mycustomdomainname.com/
* Could not resolve host: www.mycustomdomainname.com
* Closing connection 0
curl: (6) Could not resolve host: www.mycustomdomainname.com
I'm lost for things to try to get this working
HEROKU SETTINGS
I can see from inspecting my heroku Production Check, that I have the following issues:
FAILED DNS configuration
Your DNS is not configured correctly. Your domain "mycustomdomain.com"'s ALIAS or ANAME record should point to ancient-water-74339.herokuapp.com.
However, it also says:
PASSED Heroku SSL
The Heroku SSL instructions say:
Change your DNS for all domains on your app
Verify your DNS settings by running dig www.yourdomainname.com cname +short. If it returns www.yourdomainname.com.herokudns.com then you have set it up correctly. If you are using an ALIAS or ANAME record, you can verify that based on the DNS provider. For instance, DNSimple will return a TXT record showing how your domain’s ALIAS is set-up.
You should note that it may take up to 24 hours before your DNS is fully propagated globally.
To me, that means I'm supposed to replace the reference to 'herokuapp' with a reference to 'herokudns'. Also, I am reading these instructions to require that I replace 'ancient-water-74339' with a 'mycustomdomainname'.
I have done this (as I understand the instructions) so that the CNAME on my DNS now points to 'mycustomdomainname'.herokudns.com.
The heroku production check says that this is incorrect. Am I supposed to have 2 CNAME records (one using the heroku alias.herokuapp) and another using 'mycustomdomainname'.herokudns?
I tried to use the LetsEncrypt_plugin gem for rails. I cannot verify whether there is an error in that config because that gem does not provide support for heroku.
Can anyone see what I'm supposed to do to get out of this configuration hell?
The heroku production check instructions for DNS with SSL give instructions to provision an SSL Endpoint. The SSL Endpoint instructions say they are only for legacy clients (I don't know what a legacy client is), but that page suggests using the Heroku SSL instead. Those instructions say to use 'herokudns.com' on the end of the app name.
I can't seem to get any combination of these instructions to work.
Checklist
On your DNS provider make sure your CNAME record is properly set:
Host: *
Points To: yourapp.herokussl.com
How to add a CNAME record in 123-reg
On Heroku go to 'Resources' and make sure you have the SSL add-on installed.
On Heroku go to 'Settings/Domains and certificates' and make sure you have your custom domains correctly set:
Domain Name: *.yourdomain.com
DNS Target: yourapp.herokussl.com
Please note the url on herokuapp.com and herokussl.com may differ. If you are using an SSL certificate you'll have to use the latter.
Gotchas
Using the heroku toolbelt make sure your SSL certificate is still trusted
heroku certs:info --app yourapp
Your registrar and your DNS provider may differ, this may lead you to be editing your CNAME records on the incorrect place.
To make sure you are at the right spot perform a whois on your domain:
whois yourdomain.com | grep 'Name Server'
If your domain is actually pointing to 123-reg, the output should be:
Name Server: ns1-123-reg.co.uk
Name Server: ns2-123-reg.co.uk
If the output is something else, then you probably should be setting your CNAME records elsewhere.
Hope this helps <3
After pointing your domain to Heroku app on your Heroku dashboard, go to settings tab and scroll down to custom domain. There click on ADD DOMAIN and add your custom domain.
According to your explanation, the CNAME record is set up correctly for the domain.
Please make sure the domain is added from the Heroku end properly. This guide may help you https://devcenter.heroku.com/articles/custom-domains

Access localhost:3000 through mysite.loc

When I'm on my development laptop, I want to go to the URL "mysite.loc" instead of "localhost:3000" for a particular project. How can I do this?
I am using Ruby on Rails 4, and the default WEBrick server.
I have tried adding "127.0.0.1 mysite.loc" to my /etc/hosts file.
Some people suggest installing Fiddler
Add your local sitename to the HOSTS file then add a custom rule to Fiddler.
static function OnBeforeRequest(oSession: Session) {
if (oSession.host.toLowerCase() == "mysite.loc") {
oSession.host = "mysite.loc:3000";
}
}
Then when you navigate to http://mysite.loc it should be proxied to http://mysite.loc:3000.
Related questions:
ServerFault
StackOverflow
You can add a definition to your /etc/hosts file, but you will still have to use port 3000 in the URL, unless you also change this to the HTTP default (80). You could, if you really wanted, just run on port 80
sudo rails s -p 80
Not that binding to port 80 generally requires su privilege - hence the use of sudo (if available).
If you want to get straight to the desired result, with the assistance of some programming magic there is
http://pow.cx/
It does some local DNS and port magic to let you do what you are wanting.

How do I get subdomains to work on my local machine?

I cloned a repo and am trying to get it to work. The app is very heavily dependent on subdomains. For example, signin.app.com or company1.app.com.
However, since I am trying to get it to run on my local machine, I can't simply do signin.localhost:3000
So, how do I get these subdomains to work using localhost?
Thanks!
As Gene said,
/etc/hosts file:- (add)
127.0.0.1 subdomain1.localhost
And on Rails3 this is treated as a domain, so you need to just add this:
127.0.0.1 subdomain1.localhost.local
then try,
http://subdomain1.localhost:PORT
In linux
open command prompt
>sudo vi /etc/hosts
Add a line in file
127.0.0.1 subdomain.hostname.com
and press Esc and :wq (means save host file)
thats it you type subdomain.hostname.com in your browser. Subdomain will run in localhost.
You could add the domain names to your hosts file, routing them to localhost.

Setting up fake URLs locally that support subdomains

I'm developing a Rails 3 app locally on my Mac. I want to test it locally with fake URLs that support subdomains, e.g. http://blah.example.com and http://blah2.example.com. How can I set this up?
Run from your favorite shell and terminal:
sudo nano /etc/hosts
Add this in a new line:
127.0.0.1 *.example.com
Press Ctrl+X to save.
Make sure you do NOT delete the lines that are already there!

Resources