http.ListenAndServe只适用于本地主机? [英] http.ListenAndServe only works for localhost?

查看:3297
本文介绍了http.ListenAndServe只适用于本地主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  http.ListenAndServe(:80,mux)

在Go中托管我的网络服务。然而,它似乎只能与localhost一起工作。

  http.ListenAndServe(192.168.1.83:80,mux)

这适用于此地址上的特定连接,但有没有办法使其适用于服务器上的任何IP地址?b / b>

编辑: 我已经用另一个端口8080在这种情况下),然后使用:8080作为记录。即使在同一台机器上进行测试,这意味着它只能真正监听本地主机,所以似乎80端口有一些特殊之处。为避免疑惑,我使用的是Windows所有的测试都是在同一台物理机器上完成的。我也检查使用管理员权限运行,它没有区别。

:80,mux)是正确的地址。 net / http 使用 net 包。从 net.Listen() 引用:


如果主机被省略,如:8080, Listen会侦听所有可用的接口,而不是只是与给定主机地址的接口。请参阅拨号以获取有关地址语法的更多详细信息。


知道端口80受限或可能被许多系统上的防火墙阻止。在unix系统上,1024以下的端口通常需要特殊权限。例如,测试8080端口,因为这不是特殊的。

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.

解决方案

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.

这篇关于http.ListenAndServe只适用于本地主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆