我使用 golang 在 Azure Linux VM 中實現了一個 HTTP 服務器。下面是簡單的 golang 服務器代碼,偵聽端口 30175。該端口上沒有防火墻。package mainimport ( "fmt" "log" "net/http")func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])}func main() { http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(":30175", nil))}sudo netstat -tlnp 的結果是:Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1605/vsftpd tcp 0 0 127.0.0.1:3350 0.0.0.0:* LISTEN 1873/xrdp-sesmantcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1697/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1379/cupsd tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 4879/8 tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 15507/9 tcp 0 0 0.0.0.0:3389 0.0.0.0:* LISTEN 1859/xrdp tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 2112/python tcp6 0 0 :::22 :::* LISTEN 1697/sshd tcp6 0 0 ::1:631 :::* LISTEN 1379/cupsd tcp6 0 0 ::1:6010 :::* LISTEN 4879/8 tcp6 0 0 ::1:6011 :::* LISTEN 15507/9 tcp6 0 0 :::30175 :::* LISTEN 46595/HttpHandler我只能在本地主機上得到響應,但沒有來自遠程服務器的響應:curl localhost:30175Hi there, I love !curl serveripaddress:30175not working
4 回答

蠱毒傳說
TA貢獻1895條經驗 獲得超3個贊
這與您的代碼無關。這是一個典型的防火墻問題。
默認情況下(在 *nix 平臺上)阻止所有傳入流量并允許所有傳出流量。您需要打開操作系統上的端口以允許傳入流量訪問您的服務器。嘗試安裝
ufw
實用程序并運行sudo ufw allow 30175
從問題來看,您的服務器似乎正在使用 tcp6。理想情況下,它不會導致問題,因為 tcp6 應該同時支持 IPV4 和 IPV6。但我建議您將其降級為 tcp,如果這有意義的話。

SMILET
TA貢獻1796條經驗 獲得超4個贊
這是由于 Linux 監聽規則。我的規則中有一條拒絕所有規則。
# listen rules
sudo iptables -L INPUT --line-numbers
sudo iptables -D INPUT 8
- 4 回答
- 0 關注
- 213 瀏覽
添加回答
舉報
0/150
提交
取消