修改window本地hosts文件,修改域名指向


Hosts是一個沒有擴展名的系統文件,可以用記事本等工具打開,其作用就是將一些常用的網址 域名與其對應的 IP地址建立一個關聯“數據庫”,當用戶在瀏覽器中輸入一個需要登錄的網址時,系統會首先自動從 Hosts文件中尋找對應的 IP地址,一旦找到,系統會立即打開對應網頁,如果沒有找到,則系統會再將網址提交DNS 域名解析服務器進行IP地址的解析。
瀏覽器訪問網站,要首先通過DNS服務器把要訪問的網站域名解析成一個唯一的IP地址,之后,瀏覽器才能對此網站進行定位並且訪問其數據。
操作系統規定,在進行DNS請求以前,先檢查系自己的Hosts文件中是否有這個域名和IP的映射關系。如果有,則直接訪問這個IP地址指定的網絡位置,如果沒有,再向已知的DNS服務器提出域名解析請求。也就是說Hosts的IP解析優先級比DNS要高。
以上內容均從百度百科復制。
文件路徑:C:\WINDOWS\system32\drivers\etc。

如何修改hosts文件

hosts文件原內容如下:
 
# 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

 

在最后新增如下內容:

 

127.0.0.1 www.163.com

表示域名“www.163.com”指向的ip為127.0.0.1,即本地。修改后文件內容如下:

# 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
127.0.0.1    www.163.com

 

 

修改后用瀏覽器訪問“www.163.com”會被解析到127.0.0.1,導致無法顯示該網頁。

 

為了進一步驗證,使用tomcat在本地啟動一個Web服務。tomcat版本為:apache-tomcat-8.5.4,其他版本應該操作類似。

下載解壓tomcat后,運行apache-tomcat-8.5.4\bin\startup.bat,待tomcat啟動成功,本地就有了一個Web服務了。

這時再訪問"www.163.com:8080",就可以看到有頁面內容了:

為什么要加上8080呢,因為這是端口號,tomcat開啟的Web服務默認使用8080端口,而瀏覽器中默認的是80端口,所以必需要顯示指明。也可以修改tomcat安裝目錄下的config/server.xml中的8080為80,這樣就不用在訪問時顯式指定端口了。

 

這就是通過修改hosts文件來使域名指向某個IP的方式了,如果需要添加多條配置,直接再新增一行內容就行了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM