110

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.

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.

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

My /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:

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>
Adam Gries
  • 1,751
  • 3
  • 14
  • 12
  • I found this http://stackoverflow.com/questions/19313546/php-curl-is-too-slow-for-localhost. and it works for me!!! – clark yu Dec 31 '15 at 06:52
  • 2
    I reported this as a bug to apple rdar://24237290 and got this hilarious response: > Engineering has provided the following feedback regarding this issue: > This is expected behavior since the max no. of aliases in /etc/hosts file allowed is 10. Having more than 10 aliases in /etc/hosts file does not seem to be practical. I would welcome if more people report this bug and tell them that this answer is completely ridiculous (and not documented anywhere too). – dwt Feb 08 '16 at 10:41
  • 1
    Is there something bad about using the .local TLD? – yaakov Sep 16 '18 at 21:39
  • Works fine for me in mojave... :) – rogerdpack Dec 09 '19 at 18:34

14 Answers14

193

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:

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.

Jeremy Dunn
  • 2,001
  • 1
  • 12
  • 6
  • This works an I think @adam-gries should mark it as answer. Thanks Jeremy! – zysoft Jul 15 '12 at 14:23
  • 5
    This fixed worked for me but I reached a limit of 11 sites, after which it started running even slower! For now, I've split things up into two hosts entries (with the same IP address) and it seems to be going fine. – Alex Ghiculescu Apr 15 '13 at 06:46
  • Same problem and solution as @AlexGhiculescu, but in OS X 10.8 Mountain Lion. – chrishiestand Sep 03 '13 at 03:32
  • @chrishiestand: Oops, yes, should have mentioned that I'm on Mountain Lion too. Maybe they "fixed" the bug since Lion... – Alex Ghiculescu Sep 03 '13 at 03:42
  • This worked for me wonderfully! I didn't have to add my project local url to lines 2 or 3 (just like in his example). – bobber205 Oct 04 '13 at 16:22
  • 2
    OS X 10.9, still an issue and it's been driving me nuts for hours now! Brilliant, thanks for the great answer! – robertp Nov 09 '13 at 00:04
  • @Jeremy Dunn I am new to mac OSX and I am running 10.9.2 I did as you say but nothing happens, there is one part of your answer that I don't get. When you say add myproject.dev what should I exactly write instead of my project. For example if my projects address on the browser is http://localhost/ub-new I should say 27.0.0.1 localhost ub-new.dev Or am I missing something here because of being a windows user for so long? – Hooman Askari Mar 31 '14 at 08:29
  • 2
    This only works because you are entering the host entries BEFORE the other localhost device routing. If your webserver is looking at 127.0.0.1, those hosts need to be at the HEAD of the /etc/hosts file for fast lookups. I have several dozen 127.0.0.1 lines, only when I moved them to the TOP of the /etc/hosts file did they resolve fast for me. – Joey T Apr 26 '14 at 03:11
  • This worked for me following Joey T's advice above to make sure that I put my entries before the other localhost routing. – kisabelle Sep 25 '15 at 19:28
  • I found this : http://stackoverflow.com/questions/19313546/php-curl-is-too-slow-for-localhost, and it works for me. I change all my local host from "xx.local" into "xx.host" . – clark yu Dec 31 '15 at 06:53
  • Worked for me on 10.13.6 Thanks a lot. – gelviis Apr 26 '20 at 15:54
  • Worked on Catalina 10.15.6 as well. I only have two local websites to test and it took a tremendous amount of time to load. Will report back. – abhikarma1493 Aug 04 '20 at 02:43
  • 1
    Worked for me on Big Sur 11.2 ! – Raunaq Mar 04 '21 at 21:06
  • Worked for me on Monterey, wow damn that saved my life – user3491125 Mar 28 '22 at 13:19
  • Still works in MacOS Ventura 13.0.1. I think it's amazing for this to still be a thing after 7 years .. Well, what do I know about mac networking, anyways.. – Iyashi Nov 24 '22 at 22:04
102

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 Van Damme’s blog post found here.

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

“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:”

::1 mysite.local
fe80::1%lo0 mysite.local
127.0.0.1 mysite.local
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Cleverlemming
  • 1,310
  • 1
  • 8
  • 10
  • This also affects windows systems. Solution works like a charm there too. – ToBe Mar 19 '14 at 10:01
  • I kept having this issue while pointing hosts to a local VM. I left my hosts file formatted nicely *(entries on their own lines etc.)* and added additional entries, using the IPV6 address from the VM, and all my delay issues went away. I did seem to only run into this with sites ending in `.local`, and adding both IPV4/6 entries has fixed everything for me (OS X 10.9) – Justin Sep 03 '14 at 15:47
  • Had to fight with this for a little bit because my hostname in the "Sharing" preference pane did not match the one I was inputting in /etc/hosts, just in case it helps anyone... – abhishekmukherg Nov 05 '14 at 22:31
  • 1
    Wow. I understand that `::1` is the IPv6 shortcut equivalent of `127.0.0.1`. But what does `fe80::1%lo0` mean? -- ah, answered at http://superuser.com/questions/241642/what-is-the-relevance-of-fe801lo0-localhost-in-etc-hosts – AlexChaffee Apr 22 '16 at 16:58
  • on macOS Sierra this worked but I had to remove the first line. – Bryce York Sep 06 '17 at 05:03
  • 1
    You saved my day! I didn't understand why requests on my local project were so slow, with at least a 5 second response time including for the simplest HTML pages… – Michaël Perrin Sep 16 '20 at 20:08
  • OSX 10.15.7 still has this issue – Krzysztof Safjanowski Oct 25 '20 at 06:58
  • for Big Sur (11.6.7), this 5 seconds overhead only applies $name.local pattern, where $name does not contain any dot. for example, `site.local` got this overhead but `service.site.local` does not. for those who don’t mind to change host name, but want to use .local domain. – takehiro iyatomi Nov 17 '22 at 05:06
  • 2023, Mac v 11.6.7, and this seems to have solved my problem. (Hours burned looking at caching, symfony boot times, apache logs ... !) I had a few local dev sites, each of them got these three lines. Thank you very much @Cleverlemming! jz – J.Z. Jan 23 '23 at 00:52
26

I had the same problem, also on 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.

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.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
Flemming Funch
  • 379
  • 3
  • 4
  • Tried anything here in stackoverflow but this and user902664`s post helped. All, IPv4 and IPv6 lines has to be on one-line. If using IPv4 entries only but on seperated lines, decreased from 30 secs to ~ 1, using together with IPv6 settings on seperated lines decreased to ~ 0.5 secs. – tomis Apr 08 '13 at 18:00
  • This appears to be a crazy bug. I had an entry for an address, 192.168.56.3 with 14 aliases. Remove one alias and you can resolve the hosts in <1s. With the 14th alias, it takes ~30s to resolve the first few entries int he list... – Brian M. Carr Dec 04 '13 at 20:02
  • Is this a bug exclusive to OSX? Can anyone link to a bug report? – pje Mar 26 '14 at 19:08
  • Just spent hours learning more about network configuration than I ever wanted to. I was about to take the plunge and setup a local DNS server and then this fixed it. Thank you. Mavericks 10.9.5 here. – clumsyfingers Apr 06 '16 at 21:48
16

Specifying same host for IPv6 ::1 helped me.

127.0.0.1 something.local.mydomain.org
::1 something.local.mydomain.org
Aram
  • 696
  • 4
  • 16
10

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

::1 localhost

the IP v6 entries go in a separate line

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.

user902664
  • 101
  • 1
  • 3
  • 1
    This worked for me on OS X 10.11.6 - as soon as I duplicated the localhost IPv4 entry (single line for 127.0.0.1) to a ::1 line with same aliases, lookups went from 4-5 sec down to instant. I also had a 127.0.0.2 entry which I duplicated as ::2. I have a single line per address. Thanks! – RichVel May 07 '17 at 05:43
7

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

fe80::1%lo0 demo.test.dev
127.0.0.1   demo.test.dev
concept47
  • 30,257
  • 12
  • 52
  • 74
  • Agreed, works with IPv6 entry above or below IPv4 as long as it has same list of aliases. – RichVel May 07 '17 at 05:44
  • This is absurd, but fixed the problem I have had with Chrome being really slow resolving the localhost links to my laradock docker containers (Safari has always been fine) – jeff-h Apr 10 '18 at 02:26
  • Works for High Sierra as well -- Firefox, Safari work with or without it – Chris Athanasiadis Sep 28 '18 at 10:38
3

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.

Erik
  • 39
  • 1
  • This is the real solution, tried all the rest and it has nothing to do with references all on one line or linked to IPv6 route (unless maybe that's your primary route in apache conf...) thanks @Erik! – Joey T Apr 26 '14 at 02:57
  • Also this was much more than 2 or 3 seconds for me, more like 10 to 20 seconds. I had a lot of 127.0.0.1 entries but all listed AFTER the default localhost config by OS X. I am also on Lion, not ML, if that matters. – Joey T Apr 26 '14 at 03:03
  • Sadly, this no longer works w/ Mavericks - leaving my upvote here for Lion, though as it worked fine until I upgraded to Mavs a few months ago. – Joey T May 27 '15 at 01:17
1

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.

1

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:

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 alone were successful.

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

  • Changing the domain I am using from mysite.local to mysite.dev. Inspired by @Cleverlemming's answer.
  • Including the IPv6 lines.
  • Removing redundant virtualhosts and hosts (I commented them out).

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.

Community
  • 1
  • 1
Robert
  • 362
  • 2
  • 13
1

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.

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.

Aaron
  • 868
  • 1
  • 8
  • 14
0

The trick that did it for me was adding

127.0.0.1 locahost

on the first line of the host file.

From all my virtual hosts, only the ones using a database were slow. I believe it's because the process of looking up "localhost" for the database connection slowed things down, since I only added the addresses for my virtual hosts and not "localhost" as well. Now it's all snappy again. :)

D. Marti
  • 365
  • 2
  • 4
0

I've run into this a bunch, too. I have a bunch of vhosts defined on two lines, one for IPv4 and one for IPv6. Moving the host I was trying to resolve to be first in the list sped it up.

127.0.0.1 faster.example.dev host1.example.dev host2.example.dev host3.example.dev host4.example.dev host5.example.dev host6.example.dev
::1 faster.example.dev host1.example.dev host2.example.dev host3.example.dev host4.example.dev host5.example.dev host6.example.dev
Alex Dunae
  • 1,290
  • 3
  • 17
  • 28
0

A dumb issue that led me to waste some considerable time: after applying @Cleverlemming's answer, I figured out that there were duplicated entries on the hosts file. Something like:

::1          site1.local site2.local site1.local site3.local site4.local
fe80::1%lo0  site1.local site2.local site1.local site3.local site4.local
127.0.0.1    site1.local site2.local site1.local site3.local site4.local

Then IP resolving for site3.local and site4.local takes these 5-seconds of death.

nnimis
  • 464
  • 9
  • 17
0

This helped me: Apache HTTP localhost randomly taking 5 seconds on macOS Monterey but fast on HTTPS

Turn off Keep Alive by adding:

KeepAlive Off

To your http.conf