Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang http server http.ListenAndServe only works for localhost?

Tags:

tcp

server

go

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
like image 527
Junchi Liu Avatar asked Oct 19 '25 01:10

Junchi Liu


2 Answers

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”

like image 80
river Avatar answered Oct 22 '25 00:10

river


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.
like image 34
Prakhar Avatar answered Oct 21 '25 23:10

Prakhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!