0.0.0.0,127.0.0.1和localhost有什么区别? [英] What is the difference between 0.0.0.0, 127.0.0.1 and localhost?

查看:336
本文介绍了0.0.0.0,127.0.0.1和localhost有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Mac上使用 Jekyll Vagrant 。我发现 Jekyll服务器将绑定到 0.0.0.0:4000 而不是 127.0.0.1 :4000 。此外,默认情况下 gem server 将绑定到此地址。我仍然可以通过 http:// localhost:port 访问它。但对于 Jekyll 似乎默认设置(例如0.0.0.0:4000)需要访问Internet。我无法在没有互联网的情况下运行 Jekyll服务器这是一个小错误吗?

I am using Jekyll and Vagrant on my mac. I found that Jekyll server will bind to 0.0.0.0:4000 instead of 127.0.0.1:4000. Also gem server will bind to this address by default. I can still visit it via http://localhost:port. But for Jekyll, it seems that the default setting (e.g. 0.0.0.0:4000) requires Internet access. I cannot run Jekyll server without Internet. Is it a small bug?

我也用流浪。我在Vagrantfile中设置了端口转发(8080 => 4000),因为我在 Vagrant 虚拟机和测试中安装 Jekyll 它在Macintosh下。如果我使用默认设置(0.0.0.0:4000),它可以工作。我可以通过 http:// localhost:8080 从我的旅行团中访问它。但如果没有互联网,我无法绑定到0.0.0.0:4000。我使用 jekyll服务器-H 127.0.0.1 将服务绑定到 127.0.0.1:4000 ,然后我无法访问它通过 http:// localhost:8080

I also use Vagrant. I have set port forwarding(8080 => 4000) in Vagrantfile, since I install Jekyll in Vagrant virtual machine and test it under Macintosh. If I use the default setting (0.0.0.0:4000), it works. I can visit it from my safari with http://localhost:8080. But if there is not internet, I cannot bind to 0.0.0.0:4000. I use jekyll server -H 127.0.0.1 to bind service to 127.0.0.1:4000 instead, then I cannot visit it via http://localhost:8080.

任何人都可以解释0.0.0.0之间的差异,127.0.0.1和localhost ?任何人都可以解释为什么差异会导致这个问题

Can anyone explain the difference between 0.0.0.0, 127.0.0.1 and localhost? And can anyone explain why the difference will cause this problem?

推荐答案

127.0.0.1 通常是分配给环回或仅本地接口的IP地址。这是一个假网络适配器,只能在同一主机内通信。当您希望具有网络功能的应用程序仅为同一主机上的客户端提供服务时,通常会使用它。正在侦听连接的 127.0.0.1 的进程将只接收该套接字上的本地连接。

127.0.0.1 is normally the IP address assigned to the "loopback" or local-only interface. This is a "fake" network adapter that can only communicate within the same host. It's often used when you want a network-capable application to only serve clients on the same host. A process that is listening on 127.0.0.1 for connections will only receive local connections on that socket.

localhost 通常是 127.0.0.1 IP地址的主机名。它通常设置在 / etc / hosts (或在%WINDIR%下的某个名称为hosts的Windows等效项)。您可以像任何其他主机名一样使用它 - 尝试ping localhost以查看它如何解析为 127.0.0.1

"localhost" is normally the hostname for the 127.0.0.1 IP address. It's usually set in /etc/hosts (or the Windows equivalent named "hosts" somewhere under %WINDIR%). You can use it just like any other hostname - try "ping localhost" to see how it resolves to 127.0.0.1.

0.0.0.0 有几个不同的含义,但在这种情况下,当服务器被告知要监听 0.0.0.0 表示侦听每个可用的网络接口。从服务器进程的角度来看,IP地址 127.0.0.1 的环回适配器看起来就像机器上的任何其他网络适配器一样,因此服务器被告知要监听 0.0.0.0 也会接受该界面上的连接。

0.0.0.0 has a couple of different meanings, but in this context, when a server is told to listen on 0.0.0.0 that means "listen on every available network interface". The loopback adapter with IP address 127.0.0.1 from the perspective of the server process looks just like any other network adapter on the machine, so a server told to listen on 0.0.0.0 will accept connections on that interface too.

希望能回答你问题的IP方面。我不熟悉Jekyll或Vagrant,但我猜你的端口转发 8080 => 4000 以某种方式绑定到特定的网络适配器,因此当您本地连接到 127.0.0.1

That hopefully answers the IP side of your question. I'm not familiar with Jekyll or Vagrant, but I'm guessing that your port forwarding 8080 => 4000 is somehow bound to a particular network adapter, so it isn't in the path when you connect locally to 127.0.0.1

这篇关于0.0.0.0,127.0.0.1和localhost有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆