38

I finally managed how to configure properly httpd.conf for my virtualhost needings. This is the relevant part of httpd.conf file

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /Applications/XAMPP/htdocs/
</VirtualHost>

<VirtualHost *:80>
  ServerName test.local
  DocumentRoot /Applications/XAMPP/htdocs/test/
</VirtualHost>

<VirtualHost *:80>
  ServerName work.local
  DocumentRoot /Applications/XAMPP/htdocs/work/
</VirtualHost>

When I access anything on localhost (i.e. http://localhost/phpmyadmin) everything is very fast. Whenever I access test.local or work.local (or others I configured) it spends 10-15 seconds on lookup. The following requests are handled correctly and it's very fast but after a minute or so of inactivity, it has to lookup again.

This is my /etc/hosts file

127.0.0.1       localhost
255.255.255.255 broadcasthost
#::1             localhost
fe80::1%lo0     localhost

# Virtualhosts
127.0.0.1       test.local     work.local    yii.local

How could I fix this annoying issue?

5 Answers 5

84

Add your virtual hosts to the first line:

127.0.0.1       localhost test.local work.local yii.local

And remove the last line.

That should do the trick. Your vhosts are now an alias for localhost. It's not a good idea to have the same IP-address in multiple lines. This just confuses the DNS-cache.

5
  • 1
    Thanks, seems like this fixed my issue! Sep 26, 2011 at 9:45
  • I always missed that first localhost (since that part of my file i glossed over). Merging that one really helped. Nov 29, 2011 at 17:56
  • 3
    Holy **** this so enriched and saved my life! I spent without a doubt one third of the time developing locally waiting for slow loading of local virtual hosts! THANK YOU! Sep 8, 2012 at 20:34
  • Just tried the above and added my virtualhosts to the first line and made a huge difference!
    – intheusa
    Feb 8, 2013 at 22:49
  • You almost saved half of my lifetime. Love you man. I am going to vote up all your questions.
    – aBhijit
    Jul 24, 2013 at 7:49
6

What fixed it for me was editing httpd-vhosts.conf and changing all instances of:

<VirtualHost *:80>

to:

<VirtualHost 0.0.0.0:80>

It was taking about 2-5 seconds to resolve the host, now it is instant. I did not have to modify the order of my sites in my hosts file. This just makes it use ipv4 instead of ipv6 which I'd bet you don't use anyway.

5

For anyone who is using Chrome and still gets slow virtual host lookup, you need to change the virtual host name to something else than .local, eg. change test.local to test.dev.

Explanation and source here: http://bencrowder.net/blog/2012/10/slow-localhost-in-chrome/

1
  • THIS was the reason why it took so long for me !!! THANK YOU (BTW: I use Firefox on Win)
    – Enriqe
    Dec 14, 2016 at 20:51
0

You should as well implement other parameters to your vhosts file, like separate error logs and server alias


DocumentRoot "D:/xampp/htdocs/asd"
ServerName asd.com.br
ServerAlias asd.com.br
ErrorLog "logs/asd.log"
CustomLog "logs/asd.log" combined

0

Also setting the ip for ServerName in httpd.conf file worked for me

ServerName 127.0.0.1:80

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.