OS X 上的 Chrome 不尊重 etc/hosts 和/或 httpd-vhosts.conf localhost 子域

Posted

技术标签:

【中文标题】OS X 上的 Chrome 不尊重 etc/hosts 和/或 httpd-vhosts.conf localhost 子域【英文标题】:Chrome on OS X doesn't respect etc/hosts and/or httpd-vhosts.conf localhost subdomains 【发布时间】:2015-08-09 23:05:00 【问题描述】:

这是过去几天一直困扰我的情况。我在我的 etc/hosts 文件中设置了很多开发环境子域,就像这样......

127.0.0.1   localhost
127.0.0.1   sub.localhost
127.0.0.1   another.localhost
127.0.0.1   andanother.localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost

然后在我的 Applications/MAMP/conf/apache/extra/httpd-vhosts.conf 文件中指定,像这样...

NameVirtualHost *:80

NameVirtualHost sub.localhost:80
NameVirtualHost another.localhost:80
NameVirtualHost andanother.localhost:80

<VirtualHost *:80>
DocumentRoot "/Users/johndoe/websites"
ServerName localhost
</VirtualHost>

<VirtualHost sub.localhost:80>
DocumentRoot "/Users/johndoe/websites/sub"
ServerName sub.localhost
</VirtualHost>

<VirtualHost another.localhost:80>
DocumentRoot "/Users/johndoe/websites/another"
ServerName another.localhost
</VirtualHost>

<VirtualHost andanother.localhost:80>
DocumentRoot "/Users/johndoe/websites/andanother"
ServerName andanother.localhost
</VirtualHost>

最近他们都停止在 Chrome 中工作(Firefox 和 Safari 工作得很好)。我的所有 localhost 子域都开始出现 404 错误。

在 etc/hosts 中编辑/更改设置无效 编辑/更改我的 httpd-vhosts.conf 文件中的设置不起作用

我只是想不通为什么 Chrome 没有将这些地址解析到我本地机器上的那些目录。

.

【问题讨论】:

从 3 天前开始,我在使用 chromium 43 的 Ubuntu 上遇到了同样的问题。 【参考方案1】:

经过大量研究(以及反复试验),我意识到这是一个 DNS 问题。以前,我在 System Preferences > Network > Advanced... > DNS > DNS Servers 中的 DNS 设置如下...

8.8.8.8
8.8.4.4
xxx.xxx.xxx.x
xxx.xxx.xxx.x

这是前 2 个插槽中的 Google 的 DNS 服务器,x 代表我的 ISP 的 DNS 服务器地址。经过一番搜索,有些人建议先添加我的本地网络的 IP 地址,作为强制我系统上的任何东西在去外部 DNS 之前先在本地查看的一种方式,所以我将我的网络路由器的地址添加到其他地址之上...

10.0.1.1

这也不太奏效——或者确实奏效了——但没有持续(老实说现在不记得了),但我接下来尝试的实际上确实奏效了。我在本地网络地址上方添加了我的机器的本地主机(家庭)地址,就像这样......

127.0.0.1
10.0.1.1
8.8.8.8
8.8.4.4
xxx.xxx.xxx.x
xxx.xxx.xxx.x

.

现在谷歌浏览器成功地“找到”了我所有的本地主机子域。

我当然希望所有这些都对其他人有所帮助。

【讨论】:

事实证明,即使这并没有 100% 解决我的问题。然后我看到了这篇文章 - ***.com/a/10200111 - @jeremy-dunn 和他关于将所有本地域放在一行中的建议成功了!【参考方案2】:

这也困扰着我最近。它工作正常,然后突然停止了。

我将路由器的 ip 地址 (192.168.0.1) 首先放在 DNS 服务器上。

【讨论】:

以上是关于OS X 上的 Chrome 不尊重 etc/hosts 和/或 httpd-vhosts.conf localhost 子域的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Chrome 不尊重我的内容安全政策哈希?

Chrome不尊重字体粗细?

Chrome 不尊重协议相关的 URL 引用?

如何在 Firefox 中不尊重纵横比的情况下缩放 SVG 背景图像?

Chrome 中的 OS X Mavericks 推送通知

OS X 上的 AVAudioPlayer 不播放任何声音