本地电脑通过修改hosts文件实现域名本地解析

域名

开始的时候是通过ip来访问网站,但ip是一串数字不容易记忆。后来就弄了个域名。

一级域名
baidu.com
taobao.com

二级域名
www.baidu.com
image.baidu.com
三级域名
aaa.image.baidu.com

DNS服务器

Dns服务器:把域名解析为IP地址,保存的就是域名和ip的映射关系。一个域名和一个ip地址绑定,一个ip地址可以被多个域名绑定。

常用的dns服务器比如114.114.114.114

在cmd窗口中输入

cmd ping www.baidu.com

会显示

正在 Ping www.a.shifen.com [180.97.33.108] 具有 32 字节的数据

180.97.33.108就是www.baidu.com对应的那个ip地址。

修改hosts文件实现域名本地解析

自己测试的话可以本地修改hosts文件,windows的hosts文件在C:\Windows\System32\drivers\etc里边

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost

然后我再里边添加一句话,然后保存

192.168.110.128 www.163.com

当我访问www.163.com的时候就会访问我自己的nginx服务器,而不是访问网易了。

hosts文件里边只能配置ip和域名的映射关系,不能配端口号。

每次改hosts文件都要去其目录中去找,修改起来比较麻烦。可以去GitHub上去下载一个SwitchHosts来修改hosts文件,并且可以在多个hosts文件间进行切换。

我下载了个0.2.2.18.01版本,这个比较小才8M,最新的解压完都100多M了。

新建一个本地方案,填写名称,在里边填写ip和域名的映射,可以建立多个这样的方案,方便进行切换。双击方案名即可切换。

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值