What influences whether "rails server" on :3000 is accessible remotely? - ruby-on-rails

Here are two GitHub repositories:
https://github.com/maarxx/aws1T4Z
https://github.com/maarxx/aws2T4Z
Each one basically* contains the default boilerplate program that gets generated when you type "rails new APP_NAME" into a terminal.
I am deploying them identically to a remote server (specifically an EC2 instance in AWS).
For the first one, the "rails server" on port 3000 is accessible remotely.
For the second one, the "rails server" on port 3000 is NOT accessible remotely.
Would anyone be able to shed some light as to why this is the case?
To provide some background:
The first was generated in May 2014, the second was generated in March 2015.
*The first I might've tinkered with slightly, but I cannot remember what I changed (this is the driver of my question). The second is unmodified.

You may notice that your earlier project (rails 4.1.1) prints something like this at bootup:
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
Whereas, the later project (rails 4.2.0) prints something like:
=> Rails 4.2.0 application starting in development on http://localhost:3000
The difference here is that the first project is listening on 0.0.0.0 (any ip) and the second is listening on localhost (127.0.0.1), which is only accessible from localhost.
You can bind your rails server to any ip with the -b option:
rails server -b 0.0.0.0
See http://guides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server for more information on the change and How to change the default binding ip of Rails 4.2 development server? for how to change the default.

Related

Domain setting using ruby rails

I am planning to have a web application.
To do so, I studied about ruby and ruby on rails. I am using linux server from amazon clouding system.
I bought a domain from godday, and I put the IP address on DNS setting. When I run 'rails s' command, I can connect to the wep page through port 3000 in such a way that domain.com:3000. However, I cannot directly connect to domain.com. How can I my domain works without port 3000?
And Do I have to run 'rails s' every time to make the wep page work? Actually I tried to use 'rails s &' to make it run in background. But it fails. How can I make the server run even though I am not connected to the linux server?
Thank you!
usually you use rails s just in development. there are quite a few ruby web servers you can choose from for your production environment: puma, passenger or unicorn to name a few.
of course all of them have their own tutorials how to set them up. for starters, i'd go with with passenger because it's integrated with nginx and apache and easily set up.
You need to specify a port, if you don't see the port it can be either 80 (http) or 443 (https).
rails server -p 80
On linux you have to be root to bind to port less than 1000, so just append sudo in front.

Virtualbox rails server only accessible externally for some applications

I have an xUbuntu(14.04) virtualBox(4.3.15) server running on Windows 7. I have a 2 sites on the server and when I run rails server for either application it can be accessed internally at localhost:3000 without issue. However, when I access one app externally from a browser on the windows machine at the [virtualbox ip]:3000 the site renders without issue and the other displays 'cannot connect'. Additionally I can ping [virtualbox ip]:3000 for the one site, but the other will receive no response. Just the [virtualbox ip] can be pinged successfully when either site has rails server running.
Both sites are Rails 4.2.0.rc2, Ruby 2.0.0, and WEBrick 1.3.1.
Is there something that needs to be setup specifically so that the 2nd site works?
Haven't been able to locate any differences between the two which might cause an issue.
Was able to determine what the issue was. The one app was starting on http://0.0.0.0:3000 which to my understanding means it is listening on all interfaces, while the other app was starting on http://localhost:3000. The localhost app was therefore not listening for external requests. The solution was to start the rails server with the following.
rails server -b 0.0.0.0
This binds the app to the 0.0.0.0 ip address and I can now access it outside of my virtualbox xUbuntu instance by using [vm ip address]:3000 as the url.

After installing rails, how to run rails server on amazon ec2 instance?

I installed rails on an instance on amazon AMI. I installed all the dependencies for rails. I even copied the code through scp. When i ssh to the instance i ran
rails s
The server is running. How can I view it from the browser?
from the Public DNS???
In summary, sure that your security group have a rule for TCP 3000.Then, you can use the Public DNS:
rails server -b ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com -p 3000
And access to: ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com:3000
This works for me.
Your amazon ec2 instance has a public ip address and domain name, see
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html
use this domain name as the URL in your browser.
You will also have to set inbound traffic / port connections in the security group of the instance.
A word of warning: with "rails s" you are starting webrick, a server that is only meant to be used in development. You probably should use a production server like apache + mod_passenger.
For more advanced usage of ec2 and ruby on rails see
http://railscasts.com/episodes/347-rubber-and-amazon-ec2?view=asciicast
move to folder from rails aplication and typerails s -b 0.0.0.0

Rails server is running, but cannot connect to localhost:3000

I am learning Ruby on Rails with railstutorial.org
I had set everything up and working fine from Chapter 1. However, all of a sudden my next app has an issue.
I run "rails server"
=> Booting WEBrick
=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-11-15 00:45:08] INFO WEBrick 1.3.1
[2012-11-15 00:45:08] INFO ruby 1.9.3 (2012-11-10) [x86_64-linux]
[2012-11-15 00:45:08] INFO WEBrick::HTTPServer#start: pid=2752 port=3000
Seems to be working fine, just like with my previous app.
However, I try connecting to localhost:3000 , 0.0.0.0:3000 , 127.0.0.1:3000 on various browsers and they all cannot establish a connection to the server.
Some things to note:
-I was able to connect to localhost just a while ago--it just seems like it suddenly stopped working out of the blue.
-My first app was working perfectly fine, but now it doesn't work for my first app either.
-I don't have firewalls blocking the port, and my hosts file is not the problem.
-I am on Ubuntu 12.10
I almost always find solutions via search, but not this time.. so I need some help please. It's very frustrating as I feel like it's a simple problem that I spent way too long being stuck on.
Thank you.
Try running it in some other port like say 3001 as:
rails server -p 3001
If its working than than try it again on 3000 as the command above.
I thing some other software is using your 3000 port that's why its not responding.
Or for some advanced things see here
with rails 4.2.0, the server binds to localhost by default, instead of 0.0.0.0. When working with a rails in a virtual box, accessing the server from the host computer, the binding address needs to be 0.0.0.0
Start rails server with -b0.0.0.0 to make the rails server accessible from the host computer/browser.
http://guides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server
https://github.com/samuelkadolph/unicorn-rails/issues/12#issuecomment-60875268
Make sure you run rake db:create before launching rails s.
I'm using rails 5.0.0.beta3 and was running into this issue. #andrewleung's answer helped me a lot.
It seems like Rails default binding address is messed up on my computer (macOS 10.11.6) ; on some others, it works fine.
The simple solution is just to use rails server -b 127.0.0.1. You can then access your localhost:3000.
My guess here is (hinted from https://serverfault.com/a/544188) that localhost binding is messed up on my computer whereas 127.0.0.1 is more specific.
I had the same issues and i realized it was in the config/environment/production.rb file where config.assets.compile = false must be changed to config.assets.compile = true
However this might in a way render some javascript and sass elements unworking
The issue that it turned out I was having was that my VM had run out of hard drive space and there wasn't even enough left to create the server.pid file. For some reason though, it wasn't throwing an error for this, as the file was being created, but was left blank.
I run into the same issue. It turned out that browser-sync is also running on localhost:3000.
Due to some Rails developer would use browser-sync to test out the front end scripts quickly, I think that could be a popular reason that port 3000 is used.
check your /etc/hosts file..is ip 0.0.0.0 or localhost pointing to some other address.
for me...I was behind a proxy at work and had to do rails s -b 0.0.0.0 -p 3000

Why does Ruby on Rails use http://0.0.0.0:3000 instead of http://localhost:3000?

When I tried to follow the official "Getting Started" Ruby on Rails tutorial, it went wrong very quickly. Basically it said :
…navigate to http://localhost:3000. You should see Rails’ default information page.
But when I follow the instructions, I get
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
After trying both addresses, I know that they point to the same thing, but can someone explain to me why Ruby on Rails uses http://0.0.0.0:3000 instead of http://localhost:3000?
Is there a way to always have the WEBrick server use localhost?
Localhost means quite literally "your local host", usually identified by 127.0.0.1 and all traffic to that address is routed via a loopback interface. If your Web server is listening for connections on 127.0.0.1, this means that it only accepts requests coming from the same host.
0.0.0.0 means that Rails is listening on all interfaces, not just the loopback interface.
0.0.0.0 means all interfaces. Including 127.0.0.1 a.k.a. localhost.
Just so everyone knows, my firefox browser correctly displays the locally hosted server if I access
http://localhost:3000/
but it does NOT display when I attempt to access
http://0.0.0.0:3000/
as recommended by Ruby. Clearly, in some sense, they are not equivalent.
I'm on Windows btw.
If you want localhost, one quick way is to specify the binding rails s -blocalhost (and the port with -pNNNN, more options with rails s --help).
My server started running by default on localhost for reasons to be investigated. As a result lvh.me stopped working, preventing me from specifying subdomains (eg: www.lvh.me:3000).
I "solved" this specifying the binding:
rails s -b0.0.0.0 # will work with lvh.me
Rails 4.1 Warning Message.
FYI, on Rails 4.1 you will get a warning message on boot that looks like this:
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
This indicates that binding to 0.0.0.0 is not recommended and instead you should use 127.0.0.1.
In Rails 4.2+ the Rails server default binding is to localhost instead of 0.0.0.0 or even 127.0.0.1.
For those of us using Nitrous.io virtual server envrionment for development, I believe we have to bind to 0.0.0.0 as there is no localhost per se.
Restarted the os works for me. (On Mac v 10.12)

Resources