Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 339
spacemonkeys Avatar asked Feb 25 '15 19:02

spacemonkeys


2 Answers

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.

like image 119
at0mzk Avatar answered Sep 20 '22 02:09

at0mzk


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].

like image 33
Venura Athukorala Avatar answered Sep 19 '22 02:09

Venura Athukorala