What does binding a Rails Server to 0.0.0.0 buy you?
Asked Answered
M

2

57

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?

Margotmargrave answered 16/3, 2015 at 18:4 Comment(0)
V
95

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.

Velutinous answered 16/3, 2015 at 18:14 Comment(4)
You're welcome, it was a nasty (poorly publicized) gotcha for a lot of people.Velutinous
Oh man, this had me going absolutely crazy. If you're trying to run rails from a docker container and you don't specify this option, the docker container does not respond, no matter what you do. This is a lifesaver.Deci
Isnt' binding to only localhost better ? Especially if you are behind a proxy and want your site example.com to forward to localhost:3000 internally ?Scevor
I mean yes, if you have a reverse proxy that means Rails only needs to know about localhost, then you'd just run rails s without any binding (which binds to localhost). That's not what OP had.Velutinous
H
0

I think you need to use binding any time you're in a guest/virtual machine.

Hacienda answered 7/4, 2017 at 21:49 Comment(1)
It's true that binding to a non-localhost address is important in that context... it would be useful to say a lot more than this if you're wanting this answer to stand on its own, though. (And indeed, it's binding either way, it's just a question of whether it's binding to the wildcard (0.0.0.0) or localhost or what.) Downvoting for both lack of precision and lack of a full answer to the original question, though I'd be happy to re-visit given an update to the answer, because I definitely do think there's something useful in this.Gyronny

© 2022 - 2024 — McMap. All rights reserved.