4

Where do you configure the IP addresses that are permitted to connect to a Rails app?

I have a simple rails application which I have inherited that runs in a development environment on Ubuntu 14.04.

It was working OK until recently when some changes were merged in from git. Now when I run rails s the application appears to start thin as the server, as expected.

=> Booting Thin  
=> Rails 4.2.1 application starting in development on http://localhost:3000  
=> Run `rails server -h` for more startup options  
=> Ctrl-C to shutdown server  
>> Thin web server (v1.5.1 codename Straight Razor)  
>> Maximum connections set to 1024  
>> Listening on localhost:3000, CTRL+C to stop 

netstat shows that rails is in fact listening as expected

tcp        0      0 localhost:3000          *:*                     LISTEN

I can access the website ok from a browser on the server box using 127.0.0.1:3000 and all appears to work as it should. I can't access it from any other machine as a Connection Refused status is returned because rails is only allowing localhost on port 3000.

If I start Thin from the command line with thin start it returns with a similar setup

>> Using rack adapter
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

But this time Thin is listening for connections from any IP and I can reach the site from another machine. There is a difference in behaviour between starting Thin on its own and starting Thin from rails although both are version 1.5.1. Something in the Rails config is constraining Thin to listen only for connections from localhost.

I have all up to date gems so far as I can tell. I thought the issue might be something to do with eventmachine, but I can't find anything.

Any advice appreciated.

Jontiw
  • 43
  • 5
  • do you want a whitelist of ip's that can connect to your rails app while others get a message as "Not allowed to connect to his website"...something this ? – Milind May 20 '15 at 09:05

4 Answers4

4

0.0.0.0:3000 is an alias for binding to all interfaces. Localhost is literally only the localhost 127.0.0.1 which is not reachable from outside.

With rails 4 I believe they changed the deafult behavior of rails s so the server no longer listens on external interfaces. You can use

rails s -b 0.0.0.0

This will bind it to 0.0.0.0 (all interfaces),as if you started thin manually.

errata
  • 23,596
  • 2
  • 22
  • 32
1

When you start the server specify the ip on which your rails application will run by binding rails application to specific IP. This can be done with -b option. For example if your ip is 192.168.1.69

rails server -b 192.168.1.69 -p 3000
TarunJadhwani
  • 1,151
  • 7
  • 21
0

You need to allow the incoming traffic for the port 3000 when you try to access your app from other devices only with in the same network. check out the documentation for Allowing Incoming Traffic on Specific Ports on ubuntu

bhanu
  • 2,260
  • 3
  • 24
  • 35
0

Thanks errata

That explains it clearly, and now that I have the keyword "binding", I can see that this question has been asked before. It would be helpful to set the binding in a config file, but it seems this is not possible. I'll try to set an alias for the rails s command and use that.

FYI the change was made in version 4.2 of rails according to the release notes for that version because of a change in the underlying rack library.

Jontiw
  • 43
  • 5