作为Web开发者,在本地部署应用,我们都对localhost有无限的熟悉,也有无限的无奈。不管什么情况下,不同的应用都被冠以localhost:XXXX的地址,好像有性格的Web应该,都只有张三、李四这样的称呼。很多情况下,我们可能想有更个性的url来代表不同的应用,那么没有域名或者不想劳神DNS解析的情况下,让部署于本地的应该,有一个好听的url是不错的想法。那么,我们就不妨来试试。
主要的工作包括两个基本的简单步骤:
配置本机Host
在IIS中配置WebSite
配置本机的Host服务
默认情况下,Host文件位于%systemroot%\system32\drivers\etc\ 目录下, 并且只能以管理员身份进行编辑。以“Run as Administrator“打开“Notepad“,并到指定的目录打开Host文件,如下:
# 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
特别提醒,在修改Host文件前,最好对原文件进行一下备份。
然后我们就可以配置想要的域名,例如:
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 dev.anytao.net
127.0.0.2 prj.anytao.net
127.0.0.3 lab.anytao.net
上述配置都是有效的,这就意味着在本机环境下,通过lab.anytao.net就可以访问127.0.0.3的IP地址,通过localhost或者dev.anytao.net均可访问127.0.0.1地址,不过通常情况下,127.0.0.1被默认为localhost,所以,我们应该尽量避免这种配置。