Home > Back-end >  Oracle cloud - golang http server not working on port 80
Oracle cloud - golang http server not working on port 80

Time:10-21

I have a golang http server which is supposed to listen on port 80.

My problem is:

The program compiles normally but exit without listening. When I change the port to 8080 everything works normally and I can access my web page.

  • I am using Ubuntu as my operating system.
  • I added an Ingress Rules which allows port 80

I don't understand why my server is not listening on port 80 but listening on all other ports

Thank you for your help.

package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {

    fmt.Fprintf(w, "<p>Hello world!</p>")
}

func main() {

        http.HandleFunc("/", hello)
        http.ListenAndServe(":8080", nil)
}

CodePudding user response:

Check for the error

err := http.ListenAndServe(":80", nil)
fmt.Println(err)

output on my local is this, but it can have other reason

listen tcp :80: bind: permission denied

CodePudding user response:

Port 80 is a reserved / special port that must be accessed via root level users on many if not all Linux / Unix systems. You would need to run it as a root / wheel group / sudo user. Or you could run it on 8080 and reverse proxy via something that does have root level at systemd like nginx or similar. This is a common usage pattern for web applications and proxy forwarding.

I would advise against having your golang binary run as root as this is not secure at all without other safeguards in place for that user that is running as root.

nginx snippet:

  location / {
    proxy_pass http://0.0.0.0:8080;
}

To confirm that this is the issue, you can run it as sudo ./mybinary and confirm that it works and listens on 80.

CodePudding user response:

Some process on your computer is already running and listening on port 80. You can check whats listening on :80 by running "lsof -i :80"

  • Related