dongwuxie7976
2019-02-25 04:30
浏览 438
已采纳

golang http服务器http.ListenAndServe仅适用于本地主机?

I tied to implement an HTTP server in Azure Linux VM using golang. Below is the simple golang server code, listening on port 30175. And there is no firewall on that port.

package main

import (
    "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))
}

The result of sudo netstat -tlnp is:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        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-sesman
tcp        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

I can get response only in localhost, but no response from remote server:

curl localhost:30175
Hi there, I love !
curl serveripaddress:30175
not working
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongyou6909 2019-03-14 03:48
    已采纳

    This is due to Linux listen rules. There is a reject all rule on my rules.

    # listen rules
    sudo iptables -L INPUT --line-numbers
    sudo iptables -D INPUT 8
    
    点赞 打赏 评论
  • douxuan1284 2019-02-25 21:00

    You are trying to output to console. Did you try to send it as a response?

    func handler(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte(fmt.Sprintf("Hi there, I love %s!", r.URL.Path[1:])));
    }
    
    点赞 打赏 评论
  • dongyuan9149 2019-02-27 00:20

    No if you don't specify the host part of the address the server will listen on every available unicast address and every available anycast address of the system. So I would guess a problem in name resolution or routing.

    点赞 打赏 评论
  • dongtan8979 2019-03-07 19:53

    Your problem is that your server is listening on tcp6 stack. Try to explicitly use tcp with “0.0.0.0:6789” instead of just port “:6789”

    点赞 打赏 评论
  • douyanzhou1450 2019-07-09 16:16

    This has nothing to do with your code. It is a typical firewall issue.

    • By default (on *nix platform) all the incoming traffic is blocked and all outgoing is allowed. You need to open the ports on your OS to allow incoming traffic to hit your servers. try installing ufw utility and run sudo ufw allow 30175
    • From the question, it seems your server is using tcp6. Ideally, it should not cause the problem because tcp6 is supposed to support both IPV4 as well as IPV6. But I would recommend you to downgrade it to tcp, if that makes sense.
    点赞 打赏 评论

相关推荐 更多相似问题