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

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

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

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:])));
}

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.

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”

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.