今天写demo的时候碰到一个坑,用localhost无法访问,用127.0.0.1可以,查了一下,顺便记录
二者概念
- localhost:本地服务器
- 127.0.0.1:本机地址(本机服务器)
二者区别
- localhot:是不经网卡传输的,它不受网络防火墙和网卡相关的的限制。
- 127.0.0.1:是通过网卡传输的,它依赖网卡,并受到网络防火墙和网卡相关的限制。
原因分析
- localhost:系统带的本机当前用户的权限去访问
- 127.0.0.1:等于本机是通过网络再去访问本机,可能涉及到网络用户的权限。
下面是我ping出来的数据,localhost地址是::1
C:\Users\Administrator>ping localhost
正在 Ping TaoYuan-20170324 [::1] 具有 32 字节的数据:
来自 ::1 的回复: 时间<1ms
来自 ::1 的回复: 时间<1ms
来自 ::1 的回复: 时间<1ms
来自 ::1 的回复: 时间<1ms
::1 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
C:\Users\Administrator>ping 127.0.0.1
正在 Ping 127.0.0.1 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
127.0.0.1 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
解决方案
方案一
- 打开
C:\Windows\System32\drivers\etc\host
文件 - 最后面是
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
加两句,改成这样
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
localhost 127.0.0.1
现在就可以ping通了
C:\Users\Administrator>ping localhost
正在 Ping TaoYuan-20170324 [127.0.0.1] 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
来自 127.0.0.1 的回复: 字节=32 时间<1ms TTL=128
方案二
真的,特别蛋疼,如果不是上面的问题,清一下浏览器缓存试试!
网友评论