How do I use nginx with Go for a subdomain?
I have a simple program that uses http.ListenAndServe to serve content. I am using nginx to serve multiple applications on the same server and I want to use it for a go program. I tried looking for information on this, but all I found was people using FastCGI or node.js to get it to work. Can this be done with only pure Go and nginx? I understand how to use nginx with a subdomain but not with a Go program.
+3
source to share
1 answer
You can connect Nginx to your Go program directly via proxy_pass. Given:
package main
import (
"fmt"
"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)
http.ListenAndServe(":8080", nil)
}
You just need to add proxy password to nginx config:
location @go {
proxy_pass 127.0.0.1:8080;
}
+5
source to share