Golang中 怎么通过sock实现Nginx和golang程序的fastcgi通讯
type TestCgi struct {
}
//ServeHTTP xx
func (this *MPLoginCgi) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
}
func main() {
// sock目录
unixPath := "/var/www/xx/fcgi-bin/" + filepath.Base(os.Args[0]) + ".sock"
// 通过GetUnixListener() 获取一个 net.Listener
unixListener, err := tk.GetUnixListener(unixPath)
if err != nil {
rlog.Error(err.Error())
panic(err)
}
fcgi.Serve(unixListener, &TestCgi {})
}
Nginx 配置:
location ~ /fcgi-bin/(.*)$ {
#proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
#proxy_set_header X-Forwarded-Proto $scheme;
# 转发目录
fastcgi_pass unix:/var/www/xx/fcgi-bin/$1.sock;
include fastcgi_params;
}
unixPath 是监听sock 路径 ,fastcgi_pass 是把服务器请求转发给监听sock路径