Localhost is very slow

Published on Wednesday, June 1, 2011

When you write web application using PHP or ASP.NET on local PC, maybe you have installed local web server so that you can debug on local machine. However, if you are using Windows 7, you might see your localhost can not be opened in web browser, or, it is very slow to open.

The first step which you have to do is ping your localhost and ping 127.0.0.1, you can have different resolved methods due to your ping results.

For our case, we ping localhost in our Windows 7 and got the result like below:

C:\Users\Test>ping localhost

Pinging Test-Win7 [::1] with 32 bytes of data:
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms

Ping statistics for ::1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Note:
“::1” is localhost in IPv6; localhost in IPv4 is 127.0.0.1

The ping is OK. Also we ping 127.0.0.1 is OK too:

C:\Users\Test>ping 127.0.0.1

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

So looks like our localhost is OK.

Finally, we got the reason:

We were using Google Chrome and Firefox web browsers, which they have some issue when IPv6 is enabled: “Firefox and Chrome are known to be slow on localhost when IP6 is enabled.” ()

The resolution:

1: Run Notepad program as Administrator (In Windows 7);

2: Open the file named hosts in the folder C:\Windows\System32\drivers\etc\ ;

3: Take the comment sign out from this line:     # 127.0.0.1       localhost  The full content like the following:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
    127.0.0.1       localhost
#    ::1             localhost

Now you go back to your Chrome or Firefox web browser, the localhost site should be OK.