让 VirtualBox windows 来宾使用 Linux 主机的 /etc/hosts 来解析 URL

Get VirtualBox windows guest to use Linux host's /etc/hosts to resolve URLs

我正在使用 VirtualBox 并想在本地测试一些子域功能。

设置为:

主机:

客机:

互联网:

我想通过使用 Windows 来宾计算机的 IE 浏览器来测试我的应用程序,通过使用我的应用程序的实时互联网 URL,而不是转到实际的实时版本,拥有浏览器重定向到应用程序的本地版本。

通常我通过编辑 /etc/hosts 来完成此操作,目前配置为将所有实时呼叫 URL 定向到本地主机。

但目前当我使用 Windows 来宾的 IE 时,它似乎并没有读取主机的 /etc/hosts,而是直接转到在线实时网站。

解决这个问题的方法是什么?

我不确定 VirtualBox 来宾是否可以直接使用主机的 /etc/hosts,但是如果我们记住您的预期测试结果:

  1. 使用VirtualBox Windows 7/8 guest machine's IE, open internet URL http://www.yourwebsite.com
  2. 显示的不是实时在线网站,而是Linux Mint 主机的本地服务器版本

然后,一种方法是编辑 VirtualBox Windows 7/8 客户机的主机文件,将此类请求重定向到代表 Linux Mint 主机的 VirtualBox IP 地址。

根据How Do I Change My Hosts File

,这是详细信息

Windows 7台客机

Browse to Start > All Programs > Accessories.

Right-click Notepad, and select Run as administrator.

Click Continue on the UAC prompt.

Click File > Open.

Browse to C:\Windows\System32\Drivers\etc.

Change the file filter drop-down box from Text Documents (*.txt) to All Files (*.*).

Select hosts, and click Open.

Make the needed changes, ... and close Notepad.

Save when prompted.

Windows 8

相同的步骤,除了如何找到记事本。

需要的改变

hosts 中所需的更改,例如,如果您要重定向 www.yourwebsite.com:

10.0.2.2 www.yourwebsite.com

您的更改应该 take effect immediately 所以打开 IE 浏览器 http://www.yourwebsite.com 进行测试。

解释

根据 VirtualBox 文档,NAT ...is the default networking mode in VirtualBox., and with NAT, the gateway is set to 10.0.2.2

请注意网关 = 您的主机。

所以来宾通过IP访问主机,http://10.0.2.2 can be used. This can be tested in the guest machine, just open IE browser and type http://10.0.2.2

然后我们使用 Windows hosts 文件来设置自定义主机名解析,因此每当访客到达 http://www.yourwebsite.com 时,它都会重定向到 10.0.2.2,您的 Linux Mint 主机 运行 Web 应用程序的本地副本。