macos 如何在 Mac OS X Lion 上消除本地主机/虚拟主机的缓慢解析/加载(2-3 秒延迟)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10064581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:02:43  来源:igfitidea点击:

How can I eliminate slow resolving/loading of localhost/virtualhost (a 2-3 second lag) on Mac OS X Lion?

macosdnsosx-lionlocalhostvirtualhost

提问by Adam Gries

Since setting up my development environments on Mac OS X Lion (brand new macbook air purchased in January 2012), I have noticed that resolving to a virtual host is very slow (around 3 seconds) the first time but after that is fast as long as I continue loading it regularly.

自从在 Mac OS X Lion(2012 年 1 月购买的全新 macbook air)上设置我的开发环境后,我注意到第一次解析到虚拟主机非常慢(大约 3 秒),但之后很快我继续定期加载它。

If I leave it untouched for a couple of minutes and then reload again, the first reload is (again) painfully slow; seems like something is being cached.

如果我让它保持几分钟不变,然后再次重新加载,第一次重新加载(再次)非常缓慢;似乎正在缓存某些东西。

As can be seen below I am not using the .local TLD.

如下所示,我没有使用 .local TLD。

My setup: Apache 2 - MySQL - PHP installed and enabled - added a couple of virtual hosts one of which I created for localhost

我的设置:Apache 2 - MySQL - PHP 安装并启用 - 添加了几个虚拟主机,其中一个是我为 localhost 创建的

My /etc/hosts:

我的 /etc/hosts:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
127.0.0.1       myproject.dev
::1             myproject.dev
fe80::1%lo0     myproject.dev

My virtual host set-up in username.conf:

我在 username.conf 中的虚拟主机设置:

NameVirtualHost *:80

<Directory "/Users/myusername/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /Users/myusername/Dropbox/dev_envs/
</VirtualHost>
<VirtualHost *:80>
    ServerName myproject.dev
    DocumentRoot /Users/myusername/Dropbox/dev_envs/myprojectname
</VirtualHost>

回答by Jeremy Dunn

I had the exact same problem and it was driving me crazy!

我遇到了完全相同的问题,这让我发疯了!

Put all your hosts file entries for localhost into one line like so:

将 localhost 的所有主机文件条目放入一行,如下所示:

127.0.0.1 localhost myproject.dev myotherproject.dev
::1 localhost
fe80::1%lo0 localhost

Worked like a charm for me. Seems like a bug in Lion.

对我来说就像一种魅力。似乎是 Lion 中的一个错误。

回答by Cleverlemming

There's another issue 10.7.* to 10.8.4 for sites ending in ".local" which causes five second lookups. Details and solution courtesy Bram(us) Van Damme at the following link:

对于以“.local”结尾的站点,还有另一个问题 10.7.* 到 10.8.4,这会导致五秒钟的查找。详情和解决方案由 Bram(us) Van Damme 在以下链接提供:

http://www.bram.us/2011/12/12/mamp-pro-slow-name-resolving-with-local-vhosts-in-lion-fix/

http://www.bram.us/2011/12/12/mamp-pro-slow-name-resolving-with-local-vhosts-in-lion-fix/

"By default, any hostname ending in .local is treated as a Bonjour host rather than by querying the DNS server entries in Network preferences.

“默认情况下,任何以 .local 结尾的主机名都被视为 Bonjour 主机,而不是通过查询网络首选项中的 DNS 服务器条目。

To fix this problem (without having to rename each vhost) you need to add IPv6 entries for each of your vhosts in your /etc/hosts file:"

要解决此问题(无需重命名每个虚拟主机),您需要在 /etc/hosts 文件中为每个虚拟主机添加 IPv6 条目:”

::1 mysite.local
fe80::1%lo0 mysite.local
127.0.0.1 mysite.local

回答by Flemming Funch

I had the same problem, also on Lion.

我也有同样的问题,也在Lion上。

Strangely, my solution was the opposite of Jeremy's. I had a whole bunch of someproject.dev entries on one line in /etc/hosts. Loading a site on any of them the first time took forever, like a minute or so. If I used it again within 5 seconds or so it was very fast, but much longer and it would again take a minute. I had suspected all sorts of things, mysql connections, Ruby versions, Rails bugs, Apache, Phusion Passenger. Until I finally looked at the Console and realized that DNS lookups were being attempted.

奇怪的是,我的解决方案与 Jeremy 的相反。我在 /etc/hosts 的一行中有一大堆 someproject.dev 条目。第一次在其中任何一个上加载站点需要花费很长时间,比如一分钟左右。如果我在 5 秒左右内再次使用它,它会非常快,但时间要长得多,而且又需要一分钟。我怀疑过各种各样的事情,mysql 连接、Ruby 版本、Rails 错误、Apache、Phusion 乘客。直到我最终查看控制台并意识到正在尝试进行 DNS 查找。

So, I put all of them on seperate lines:

所以,我把它们都放在了单独的行上:

127.0.0.1 localhost

127.0.0.1 myproject.dev

127.0.0.1 myotherproject.dev

And suddenly everything was snappy again. Same on both my machines.

突然间,一切又变得活泼起来。我的两台机器都一样。

回答by Aram

Specifying same host for IPv6 ::1 helped me.

为 IPv6 ::1 指定相同的主机对我有帮助。

127.0.0.1 something.local.mydomain.org
::1 something.local.mydomain.org

回答by user902664

Make sure to put the IP v6 entries not in the line with localhost

确保将 IP v6 条目与 localhost 不在同一行

::1 localhost

the IP v6 entries go in a separate line

IP v6 条目在单独的行中

fe80::1%lo0 here and_here

It is sometimes really fast now, but there are rare exceptions where the old lags come back. They might however be based on other reasons.

现在有时真的很快,但也有罕见的例外,旧的滞后又回来了。然而,它们可能基于其他原因。

回答by concept47

On OSX El Capitan what worked for me was making a duplicate IPv6 entry right above the IPv4 entry like so

在 OSX El Capitan 上,对我有用的是在 IPv4 条目正上方创建一个重复的 IPv6 条目,就像这样

fe80::1%lo0 demo.test.dev
127.0.0.1   demo.test.dev

回答by Erik

Ensuring that the host names are defined at the beginning of the file made the difference for me. By default the line 127.0.0.1 localhost is already at the beginning, just add your entries on the same line.

确保在文件开头定义主机名对我来说很重要。默认情况下,127.0.0.1 localhost 行已经在开头,只需在同一行添加您的条目​​。

回答by Finn Espen Gundersen

I had the same problem and found it to be caused by enabling IPv6 on my LAN, but not having IPv6 configured correctly between my network and my ISP. Apparently the IPv6 DNS-server takes precedence over IPv4 DNS when the client is given both. It took a couple of seconds (on every attempt) for the client to find that the IPv6 DNS was unreachable or missing, and then falling back to IPv4 DNS.

我遇到了同样的问题,发现它是由于在我的 LAN 上启用 IPv6,但没有在我的网络和我的 ISP 之间正确配置 IPv6 引起的。显然,当客户端同时获得两者时,IPv6 DNS 服务器优先于 IPv4 DNS。客户端花了几秒钟(每次尝试)发现 IPv6 DNS 无法访问或丢失,然后回退到 IPv4 DNS。

回答by Aaron

I had this same problem and finally realized I had the same host entry twice on the same line:

我遇到了同样的问题,最终意识到我在同一行上两次使用相同的主机条目:

e.g.

例如

127.0.0.1 localhost host1 host2 host3 host4 host5 host1 host6

I removed the second instance of the same host (in the example above - host1) - and things immediately sped up.

我删除了同一主机的第二个实例(在上面的示例中 - host1) - 事情立即加快了速度。

Felt a little silly when I discovered this, but when you've got 10 long host names on the same line and you're frequently adding / removing, it can be eaisly overlooked.

当我发现这一点时觉得有点傻,但是当您在同一行上有 10 个长主机名并且您经常添加/删除时,它很容易被忽略。

回答by Robert Deniszczyc

Note: I am using Windows and XAMPP, however while researching the problem many people have had the same issue on Windows and Mac. Answer for reference for anyone finding this question as I have spent hours trying to find a solution that works for me:

注意:我使用的是 Windows 和 XAMPP,但是在研究该问题时,许多人在 Windows 和 Mac 上遇到了同样的问题。为找到此问题的任何人提供参考答案,因为我花了数小时试图找到适合我的解决方案:

I have tried many solutions for the same problem including putting all of the hosts on one line, removing redundant hosts and virtualhosts, and also including the IPv6 lines - none of these alonewere successful.

我已经尝试了同样的问题,很多解决方案,包括把所有的主机在同一行,去除冗余主机和virtualhosts,并且还包含IPv6线-没有这些的单独成功。

The only solution which has so farappeared to work for me is a combination of all of the solutions:

到目前为止似乎对我有用的唯一解决方案是所有解决方案的组合:

  • Changing the domain I am using from mysite.localto mysite.dev. Inspired by @Cleverlemming's answer.
  • Including the IPv6 lines.
  • Removing redundant virtualhosts and hosts (I commented them out).
  • 从 mysite 更改我正在使用的域。本地到 mysite。开发。灵感来自@Cleverlemming 的回答。
  • 包括 IPv6 线路。
  • 删除冗余虚拟主机和主机(我已将它们注释掉)。

In my hosts file my hosts are currently on separate lines and so far the issue seems to be fixed.

在我的主机文件中,我的主机目前位于不同的行上,到目前为止问题似乎已解决。

Good luck to anyone attempting to solve this issue and if anyone has any information to add please do so - this seems to be an issue affected a lot of people with no single known cause or solution.

祝任何试图解决这个问题的人好运,如果有人有任何信息要添加,请这样做 - 这似乎是一个影响了很多人的问题,没有单一的已知原因或解决方案。