简体   繁体   English

为什么 Ruby on Rails 使用 http://0.0.0.0:3000 而不是 http://localhost: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.当我尝试按照官方“入门”Ruby on Rails 教程进行操作时,很快就出错了。 Basically it said:基本上它说:

…navigate to http://localhost:3000. …导航到 http://localhost:3000。 You should see Rails' default information page.您应该看到 Rails 的默认信息页面。

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 ?在尝试了这两个地址之后,我知道它们指向同一个东西,但是有人可以向我解释为什么 Rails 上的 Ruby 使用http://0.0.0.0:3000而不是http://localhost:3000 :700D9A 吗?

Is there a way to always have the WEBrick server use localhost?有没有办法让 WEBrick 服务器始终使用 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. Localhost 的字面意思是“您的本地主机”,通常由 127.0.0.1 标识,所有到该地址的流量都通过环回接口路由。 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.如果您的 Web 服务器正在侦听 127.0.0.1 上的连接,这意味着它只接受来自同一主机的请求。

0.0.0.0 means that Rails is listening on all interfaces, not just the loopback interface. 0.0.0.0 意味着 Rails 正在侦听所有接口,而不仅仅是环回接口。

0.0.0.0 means all interfaces. 0.0.0.0表示所有接口。 Including 127.0.0.1 aka localhost .包括127.0.0.1又名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.众所周知,如果我访问http://localhost:3000/ ,我的 firefox 浏览器会正确显示本地托管服务器,但当我尝试访问http://0.0.0.0:3000/时,它不会按照 Ruby 的建议显示。 Clearly, in some sense, they are not equivalent.显然,在某种意义上,它们并不等价。

I'm on Windows btw.顺便说一句,我在 Windows 上。

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 ).如果你想要localhost ,一种快速的方法是指定绑定rails s -blocalhost (以及带有-pNNNN的端口,带有rails s --help更多选项)。

My server started running by default on localhost for reasons to be investigated.由于需要调查的原因,我的服务器默认开始在localhost上运行。 As a result lvh.me stopped working, preventing me from specifying subdomains (eg: www.lvh.me:3000 ).结果lvh.me停止工作,阻止我指定子域(例如: 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. Rails 4.1 警告消息。

FYI, on Rails 4.1 you will get a warning message on boot that looks like this:仅供参考,在 Rails 4.1 上,您将在启动时收到一条警告消息,如下所示:

=> 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 .这表明不推荐绑定到0.0.0.0 ,而应该使用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 .在 Rails 4.2+ 中,Rails 服务器默认绑定是到localhost而不是0.0.0.0甚至127.0.0.1

实际上,rails 有不同的配置选项,关于它是侦听特定接口还是所有接口。

对于我们这些使用 Nitrous.io 虚拟服务器环境进行开发的人,我相信我们必须绑定到 0.0.0.0,因为本身没有 localhost。

Restarted the os works for me.重新启动操作系统对我有用。 (On Mac v 10.12) (在 Mac v 10.12 上)

use this: rails server -u webrick使用这个:rails server -u webrick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Puma 监听 'tcp://localhost:3000' 而不是 'http://localhost:3000' - Why Puma listen on 'tcp://localhost:3000' instead of 'http://localhost:3000' Ruby on Rails:http:// localhost:3000无法正常工作 - Ruby on Rails: http://localhost:3000 not working Ruby on Rails在Windows上无法连接到localhost:3000、127.0.0.1:3000和0.0.0.0:3000 - Ruby on Rails can't connect to localhost:3000, 127.0.0.1:3000 and 0.0.0.0:3000 on Windows Ruby on Rails无法访问MAC上的http:/ localhost:3000 - Ruby on rails cannot access http:/localhost:3000 on MAC 为什么在Ruby on Rails上,http:// localhost:3000 / stories / newnew将查找记录id = newnew? - why on Ruby on Rails, http://localhost:3000/stories/newnew will look for record id=newnew? 在Ruby on Rails中,为什么http:// localhost:3000 / foobars / alt / 1.xml不起作用? - In Ruby on Rails, why http://localhost:3000/foobars/alt/1.xml not work? Ruby on Rails设计http://0.0.0.0:3000/users/sign_out收到路由错误 - Ruby on Rails devise http://0.0.0.0:3000/users/sign_out gets routing error 无法使用Rails ping http:// localhost:3000 - cannot ping http://localhost:3000 with Rails 无法打开http:// localhost:3000-为什么? - Can't open http://localhost:3000 - why? 无法访问http:// localhost:3000 - Cannot access http://localhost:3000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM