networking - Why does localhost not work in Windows 8.1, but 127.0.0.1 does?

14
2014-06
  • cja

    I have a HTTP server running on port 9876.

    I put http://127.0.0.1:9876/ into Chrome and the page loads instantly.

    I put http://localhost:9876/ into Chrome and the page never loads.

    I have never modified C:\Windows\System32\drivers\etc\hosts.

    Every line in the hosts file is commented out.

    The following lines exist in the file and may be relevant:

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

    As I said, I have not added these lines myself. I have not edited the hosts file.

    Obviously I can uncomment the second line to make localhost work. But why is this necessary if the first line is correct? Did Microsoft write the first line? It looks like they're telling me that localhost will work without being defined.

  • Answers
  • Steve Butler

    I can't find reliable documentation, but it appears since windows 7 they changed localhost resolution to DNS. I'm guessing the DNS client service actually hands back localhost when you do an operation, because your DNS server, where ever it is, probably wont resolve localhost. You can always set the hosts file, and it aught to override what the DNS client gives you.

    Here's at least proof that it changed since windows 7:

    http://support.microsoft.com/kb/972034

    I'm assuming its the DNS service, because on my Win8 box, those lines are commented, and yet localhost resolves back to ::1 and of course my DNS is not configured for IPv6, and even if it were, it wouldn't resolve localhost.

  • Poliquin

    If you remove the second '#', localhost should work as well.

    from

    # 127.0.0.1     localhost
    

    to

    127.0.0.1     localhost
    
  • David Schwartz

    The comment is talking about the DNS implementation in Windows itself, not the DNS implementation in Chrome.


  • Related Question

    mac - Safari is unable to reach localhost (127.0.0.1)
  • knorv

    I've encountered a really strange Mac problem: Safari is unable to connect to localhost whereas all other applications can connect as normal (Firefox, Chrome, ping, etc.).

    My /etc/hosts is the standard setup:

    $ egrep localhost /etc/hosts
    # localhost is used to configure the loopback interface
    127.0.0.1   localhost
    ::1         localhost 
    fe80::1%lo0 localhost
    127.0.0.1   testing.localhost
    

    Question: Have anyone else encountered this? What could be the problem?

    Update #1: I have a server running on localhost:8080. When accessing from Safari I get the error message "Safari could not connect to the server". "http://localhost:8080/" works flawlessly in Firefox and Chrome.

    Update #2: Same problem with "http://127.0.0.1:8080/" - so it does not seem to be related to resolving localhost to 127.0.0.1.


  • Related Answers
  • EvilChookie

    I think we need a little more information based on your question.

    I'm going to guess you have a web server running to accept requests? I currently don't, and Safari won't connect to 127.0.0.1, because it "Couldn't find 127.0.0.1" - even though I can ping it just fine.

    So first, you need to have something running. Secondly, have you tried connecting to testing.localhost with Safari, just incase the two entries are confusing Safari (this is entirely possible).

    When you say "applications can connect as normal" with Chrome and Firefox, what are you seeing? The web page you're expecting to see? A different error?

    Edit

    I downloaded and installed MAMP, and I could not reproduce this error.

    Some forum threads suggest IPv6 resolution is causing this issue. I would be inclined to turn off IPv6 and remove the entry from the hosts file (temporarily just for testing)

    1. Apple Menu > System Preferences
    2. Network
    3. Select your Network Connection > Advanced
    4. Configure IPv6 > Off

    Apply, restart, and see how it goes.

  • ericvg

    I've had this happen before -- using http://computername.local/ worked for me, but I'm unsure why it would be different than just using a name in a host file.

  • grawity

    Some applications decide to bind to only a single interface - 192.168.13.37, for example, if that is the address assigned to eth0 or such - instead of using 0.0.0.0 for all interfaces.

    Others might be using only IPv6 addresses ::1 (localhost) or :: (all interfaces). Try http://[::1]:8080/ in this case.

  • pixeline

    i'm not going to answer your question directly, but offer an alternative solution: use virtualhostx to manage the local DNS. See also if MAMP is not a good solution for your local development needs (if that's what you're after).

  • Snark

    Comment out the line:

    ::1         localhost
    

    from your /etc/hosts file. Apparently this would be caused by IPv6 support in Safari.

  • Arjan

    To ensure it's not an issue with your web server (like @nagul asked: what server are you using?), on my 10.6 Snow Leopard with IPv6 left at automatic, and even with 127.0.0.1 testing.localhost added to my /etc/hosts, the following works fine:

    • Change the Listen port of the built-in Apache from 80 to 8080:
      sudo vi /etc/apache2/httpd.conf
    • Run the built-in Apache by enabling System Preferences, Sharing, Web Sharing. Or restart using: sudo apachectl restart

    Likewise, for me no problems using:

    • python -m SimpleHTTPServer 8080

    I know you stated you're not using any proxy. Just in case there's any doubt: recent versions of Firefox allow you to choose between Firefox-specific network settings (which was the only option in older versions), and the system settings. Safari always uses the system settings. Of course, good proxy settings would ignore local addresses (defaults on my Mac for Bypass proxy settings for these Hosts & Domains: *.local, *.lan, 169.254/16). Still, one could check if Firefox still works when making it use the system settings just like Safari. (Firefox Preferences, Advanced, tab Network, button Settings.)

    (Anything in the server logs or Console logs? Which version of Mac OS?)

  • alexus

    What's your DNS? some DNS providers like OpenDNS depends on your settings might prevent you from going somewhere, so to test it completely remove DNS and make sure you have no proxy configured as well

  • aaron

    it sounds like your server may not be running on the ip and port you're expecting. what is output when you run the following in a terminal?

    netstat -anp tcp | grep 8080
    

    you should see a line corresponding to your server. if no line appears, then your server may be running on a different port.