MacOS Sierra – localhost not found while using Hugo (not related to Apache)

dnsNetworkwebserver

note this is NOT related to Apache and I'm not looking for Apache-centered answers.

I am using the Hugo static site generator to create sites and trying to test on a MacBook running Sierra.

I am able to start the server with

hugo server -Dw

And the server starts on localhost:1313, however when I try to access that URL I get a 404. 127.0.0.1:1313 works, but then all of the links fail as they try to resolve back to localhost.

I was able to access by enabling a domain name on my router (foo.home) and then I can open the main page with localhost.foo.home:1313, but all of the Hugo links still try to use localhost:1313.

So, two questions:

  1. Can I change the behavior of Hugo to use 127.0.0.1? I tried changing
    the baseURL in config.toml to localhost, 127.0.0.1 and the foo.home
    domains, but that didn't seem to do anything.
  2. Or, can I change the /etc/hosts file to resolve localhost?

Right now /etc/hosts has four entries – I am wondering if I can add another entry for 127.0.0.1 localhost but there's a warning about not making changes as it will break configuration of the loopback interface.

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

Best Answer

The key to this error was the following:

127.0.0.1:1313 works, but then all of the links fail as they try to resolve back to localhost.

That gave the hint - accessing via IP works but not via hostname. Thankfully, the /etc/hosts file was also included for examination. In it we see the following line (1st line):

127.0.0.1       localhostw

The trailing "w" at the end of the hostname localhost will cause the error. In effect, localhost wasn't defined, but localhostw was.

Removing the "w" will solve the issue.