Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http.ListenAndServe only works for localhost?

Tags:

http

server

go

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.

like image 591
Saurbaum Avatar asked Oct 15 '16 19:10

Saurbaum


1 Answers

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.

like image 124
icza Avatar answered Oct 17 '22 15:10

icza