http.ListenAndServe only works for localhost?

11,487

http.ListenAndServe(":80", mux) is the correct address. net/http uses the net package. Quoting from net.Listen():

If host is omitted, as in ":8080", Listen listens on all available interfaces instead of just the interface with the given host address. See Dial for more details about address syntax.

Know that port 80 is restricted or might be blocked by firewalls on many systems. On unix systems ports under 1024 usually require special permissions. Test the 8080 port for example, because that is not special in this way.

Share:
11,487
Saurbaum
Author by

Saurbaum

A C++ programmer who somehow fell into C# and the world of web programming. If someone creates another mobile OS I may just scream.

Updated on June 17, 2022

Comments

  • Saurbaum
    Saurbaum almost 2 years

    I've been successfully making use of

    http.ListenAndServe(":80", mux)
    

    to host my web service in Go. It only appears to work with localhost however.

    http.ListenAndServe("192.168.1.83:80", mux)
    

    This works for specific connections on this address but is there a way to make it work for any ip address on the server?

    Edit:

    I've checked it with a different port (8080 in this case) and then using ":8080" works as documented. There appears to be something special about port 80 even when testing on the same machine that means it only actually listens on localhost.

    For the avoidance of doubt I'm using Windows and all testing is done on the same physical machine. I've also checked running with admin privileges and it makes no difference.