Problem

I used to have a bunch of domains in /etc/hosts like peterbecom.dev for testing Nginx configurations locally. But then it became impossible to test local sites in Chrome because an .dev is force redirected to HTTPS. No problem, so I use .local instead. However, DNS resolution was horribly slow. For example:


▶ time curl -I http://peterbecom.local/about/minimal.css > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  1763    0     0    0     0      0      0 --:--:--  0:00:05 --:--:--     0
curl -I http://peterbecom.local/about/minimal.css > /dev/null  0.01s user 0.01s system 0% cpu 5.585 total

5.6 seconds to open a local file in Nginx.

Solution

Here's that one weird trick to solve it: Add an entry for IPv4 AND IPv6 in /etc/hosts.

So now I have:

cat /etc/hosts | grep peterbecom
127.0.0.1       peterbecom.local
::1             peterbecom.local

Verification

Ah! Much better. Thing are fast again:


▶ time curl -I http://peterbecom.local/about/minimal.css > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  1763    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl -I http://peterbecom.local/about/minimal.css > /dev/null  0.01s user 0.01s system 37% cpu 0.041 total

0.04 seconds instead of 5.6.

Comments

Post your own comment
Michał

This is fantastic, thank you!

Jason

Finally! This was killing me. Thank you so much.

Roberto Fierros Z

Huge thanks!

Mihai

This was killing me too. Thank you!

Edgars

Thank you very much!

César Couto

This was actually incredibly helpful.
I was using Prepos to live preview my websites in localhost, actually localhost was okay with prepos it was taking forever to resolve and this fixed my problem.

Floris

Wow, thank you. This made localhost like 3 times faster for me. Awesome!

ivastly

1000x faster for me, many thanks!

Adam Patterson

Would love to know if this is still true, I had this exact setup before updating to Catalina and now that I have it's frustratingly dirt slow!

TacoTaco

Worked on Catalina. Thanks.

Nate Barbettini

Thanks, this saved me hours of frustration! I'm not sure what caused this to start (I didn't have ::1 in /etc/hosts for a long time).

Rich

This is a life-saver! Thank you!

Anonymous

Thank you so much, I was going nuts :P

Gary

What is life?! I've been blaming 'Docker For Mac' for this speed issue like an absolute plum, although random 300% CPU usage incurs some legitimate blame too. Thanks for this bit of life-saving wisdom!

@timmywheels_

Thank you for this! Muchhhhh faster now.

aduckdev

thank, life saving , should be added to Docker documentation

Kurt

Glad I bumped into this article. Thanks!

Brian

This is still fixing issues in 2022. Thanks! (Was linked to from StackExchange: https://superuser.com/questions/525664/local-dns-resolution-really-slow-on-mac-os-x)

Nickolay Ponomarev

::FFFF:192.168.x.y for non-localhost IPs works as well.

Your email will never ever be published.

Related posts

Go to top of the page