我有一个简单的go程序,它使用http.ListenAndServe来提供内容。我使用nginx在一台服务器上为多个应用程序提供服务,我也想将其用于go程序。我试图寻找有关它的信息,但是我发现所有使用FastCGI或node.js使其正常工作的人。仅使用Go和nginx可以做到吗?我了解如何将Nginx与子域一起使用,但不能与Go程序一起使用。
您可以直接通过proxy_pass将Nginx连接到Go程序。鉴于:
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)
}
您只需要将proxy_pass添加到您的nginx配置中:
location @go {
proxy_pass 127.0.0.1:8080;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句