I cannot connect to rails server. localhost:3000 - ruby-on-rails

I just using AWS, EC2.
and I run server but I cannot access localhost:3000.
I tried
rails server -b0.0.0.0
rails server -b(AWS public host)
how can I access to server...??
enter image description here

Yes rails server -b 0.0.0.0 is correct, you should be able to access it from the outside on port 3000 if your AWS environment is set up correctly (hint, check firewalls etc),
on the other hand, why would you use port 3000 to access an AWS hosted web service from the outside is a completely different matter

On your screenshot, you are not trying to reach "localhost" but your public IP. Can you try http://localhost:3000 while running rails server -b 0.0.0.0?
To access your public IP, I assume that your machine is accessible publicaly on port 22 since you ssh'd in it. So just make sure that you have opened your security group on port 3000 (and also that you don't have any DENY ACL on your subnet, but this should be OK by default if you didn't touch the ACL rules).

Related

Connecting rails server from local machine to network

I have made a rails application using 4.2.0 in my local Ubuntu machine. I want to deploy that application to local network of my campus. How can I do that?
I checked my ip from ifconfig, and deployed the webrick server via binding that ip and different ports too,
rails server -b my_ip -p port
the console says that the application is running in that ip and port but when I am trying to connect it via my browser, I get service timout error. Can anybody suggest me a method to do that?
Binding to 0.0.0.0 tells the service to bind to all IP addresses on your machine. Rails server used to do this by default, but with 4.2 changed to binding only to localhost.
Basically if it's only bound to localhost then it will only respond locally to either localhost or 127.0.0.1 which can't work through a DNS service because it's not a public IP address.
When you use 0.0.0.0 it will bind to localhost and to your routable IP address.
Use rails s -b 0.0.0.0

What does binding a Rails Server to 0.0.0.0 buy you?

I am using "www.xip.io" as a DNS wildcard for testing on different devices. I set my primary domain to my IP address. I fire up a rails server with bundle exec rails server and I go here www.<ip_address>.xip.io:3000 and notice my rails server doesn't respond.
However, if I bind my rails server to 0.0.0.0 like so bundle exec rails server -b 0.0.0.0, it works! I don't understand what 0.0.0.0 is telling my server for this to work. Can someone make sense of this?
Binding to 0.0.0.0 tells the service to bind to all IP addresses on your machine. Rails server used to do this by default, but with 4.2 changed to binding only to localhost.
Basically if it's only bound to localhost then it will only respond locally to either localhost or 127.0.0.1 which can't work through a DNS service because it's not a public IP address.
When you use 0.0.0.0 it will bind to localhost and to your routable IP address.
I think you need to use binding any time you're in a guest/virtual machine.

No access to a rails server running behind a firewall

I am trying to run a ror application at a friend's home and the application is not reachable from the outside. Here is what we have tried.
The application has been created with
$ rails new <application name>
and started with
$ rails server
The development server starts and is reachable on http://localhost:3000
Next thing we have activated port forwarding on the router he uses to connect to the internet: we have mapped the external port 3000 to port 3000 on the internal host on which the rails server is running. When I try to open the page from the outside (with firefox) I get an error page: unable to conenct to host, the host may be down, try to connect later.
Things we have tried:
Changed port forwarding 80 -> 80
Installed and started Apache: the default Apache page is reachable.
Stopped Apache and started the rails server on port 80 (with sudo, otherwise it can't use port 80): the rails server is not reachable.
I have also checked the firewall of the machine running the rails server. The OS is ubuntu 12.04: the firewall (at least ufw) is not active. I can also connect to that machine using port 22 / ssh.
Summarizing:
rails server is accessible on port 3000 on local host
port forwarding works for ports 22 and 80 and both ssh server and apache server are reachable
rails server is NOT reachable on both port 80 and 3000 from the outside
The only thing I can think of is that rails might have a mechanism similar to Django's ALLOWED_HOSTS and that it refuses external requests because they are for a different host than localhost. However, I haven't found anything on this topic. Also, the application's log files do not show any connection attempt that was refused by the rails server.
So we are out of ideas. What should we check next?
Prior to Rails 4.2, the default for rails server was to bind to all interfaces. In Rails 4.2 this changed to only bind to 127.0.0.1 by default - there's nothing in the application logs because the socket is simply not listening to connection on other network interfaces.
To allow connections from another machine you need to use the -b option to bind to extra ip addresses, eg
rails s -b 0.0.0.0
to bind to all available ip addresses. You can of course replace 0.0.0.0 with one of your machine's actual ip addresses although you would of course have to change that invocation whenever your machine's ip address changed.

Can't connect to Rails server running on EC2 from public IP

I recently have been having a problem with my AWS EC2 instances. The problem is that I cannot hit my Rails servers via public IP, but I can hit localhost and the server will respond.
Here's what I'm doing:
Create new EC2 instance (t2.micro, ubuntu free tier)
Security group has port 22, 80, 3000 open to everyone (0.0.0.0)
SSH to EC2 instance, install rails (I've been using this to install)
Start rails server after install, it's running on port 3000
run "wget localhost:3000" and it returns index.html, yay!
go to my web browser, type in EC2 instance public IP and port 3000 (IP:3000), says it can't connect :(
kill rails server, restart it on port 80, wget works but can't connect via public IP
as a sanity check, I install nginx and run it, and can see the nginx start page on port 80 via public IP... so confused...
So I'm thinking it has something to do with how I'm installing Rails, but I've tried methods other than using that install script but am encountering the same problem... I've even tried creating an entirely new AWS account just in case I screwed up the settings in my original account but haven't had any luck. I have previously been able to get rails running on EC2 instances just fine (in fact I have EC2 instances using the same security group running on my AWS account right now and can hit those public IPs just fine), but am now just banging my head against the wall... any help would be greatly appreciated!
EDIT: For now, I've configured nginx to hit my rails server.... at least that works for now... although I'm still curious why I can't hit my rails server directly...
Check if rails listens on 0.0.0.0 or 127.0.0.1, default is to listen only on localhost.
-b, --binding=IP Binds Rails to the specified IP.
Default: localhost
From the Ruby on Rails 4.2 Release Notes:
Due to a change in Rack, rails server now listens on localhost instead of 0.0.0.0 by default. This should have minimal impact on the standard development workflow as both http://127.0.0.1:3000 and http://localhost:3000 will continue to work as before on your own machine.
If you are using 'rails server' and want to use the default port 3000 then use below:
sudo rails server -b 0.0.0.0
it can be accessed as http://[public_ip]:3000.
If you want to use it as a URL without the port use the below to run the server:
sudo rails server -p 80 -b 0.0.0.0
You just access it as http://[public_ip].
I was running the rails server on default port 3000 on my AWS Ubuntu instance and had allowed this port in the security group, inbound section but still I was unable to access my application by using this.
http://public_ip:3000
I searched a lot and this answer helped me. Explaining the answer. We have to make firewall setting flexible so that our firewall allows port 3000.
You can use this command to check if this port is allowed on firewall or not.
$ sudo iptables -L | grep :3000
If you see nothing it means we have to allow it using this command.
$ sudo iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
This worked for me to get my rails app access on port 3000.

Rails server won't respond to any request outside the local network

I'm running Rails con a VM with a bridged interface, it has it's own IP in the local network.
When I start rails in 0.0.0.0:3000 I can access it using the local network IP in the port 3000 without any problem.
The router NAT configuration is to route the port 3000 to the VM.
When I check the port 3000 on http://canyouseeme.org with the Rails server stopped it says the port is closed. With the rails server started it says the port is open. This means, the router is indeed routing to the VM, right?
But when I try to enter the server through my public IP in the port 3000, it won't answer.
I tried changing the server from Thin to Webrick, with the same result.
Also, I tried to run another web server in the port 3000 with he same results. No answer to public requests outside the local network. Yet the port is checked as opened.
Also, UFW is stopped, and anyway I allowed the port 3000.
Any ideas?
Some routers don't support loopback, meaning you can't access the public IP from within the LAN. FWIW, I tried this"
telnet canyouseeme.org 3000
Trying 8.23.224.110...
With no luck.

Resources