1.配置Nginx/FastCGI
This is actually pretty easy. I assume you already have some experience configuring Nginx.conf. (Each install seemingly has different defaults as to the conf file's location,and contents,so I won't go over it here. Mine is in /usr/local/etc/Nginx.)
I assume too that you've configured PHP with FastCGI before. If not,you may still understand what's happening.
All you have to do is tell Nginx to pass certain requests,or maybe all of them if you wish,to FastCGI on a certain port. Our Go program will have a FastCGI handler listening on that same port. If you need a reference,my entire server { ... }block looks like this:
server {
listen 80;
server_name go.dev;
root /root/go/src/godev;
index index.html;
#gzip off;
#proxy_buffering off;
location / {
try_files $uri $uri/;
}
location ~ /app.* {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9001;
try_files $uri $uri.html =404;
}
Notice that the
server_nameis
go.dev. I added this to my hosts file with the loopback IP address,127.0.0.1. So when I type http://go.dev in my browser,it resolves to my own Box and Nginx receives the request.
Also notice that the
fastcgi_passport is not the default 9000,but is 9001. I did this because I already have PHP-fpm listening on port 9000. My Go app will listen on 9001.
Further notice that the
fastcgi_passis in a
location ~ { ... }block such that any page under
/appof
go.devwill be redirected to my Go program. You can change this path to whatever you'd like or even replace it with the
location / { ... }block above if you want your Go program to be executed out of the root of the site.
Let's see it working
Make a .go file (I'm calling mine app.go) with these contents (though I encourage you to study why/how it's working):
package main
import (
"net"
"net/http"
"net/http/fcgi"
)
type FastCGIServer struct{}
func (s FastCGIServer) ServeHTTP(resp http.ResponseWriter,req *http.Request) {
resp.Write([]byte("<h1>Hello,世界</h1>\n<p>Behold my Go web app.</p>"))
}
func main() {
listener,_ := net.Listen("tcp","127.0.0.1:9001")
srv := new(FastCGIServer)
fcgi.Serve(listener,srv)
Here's what's happening: The main() function creates a network listener at localhost on port 9001,which is our FastCGI pass-thru port from the Nginx config. Then we make a new FastCGIServer and serve requests that pop into that port. The
fcgi packagehas one function: Serve,which blocks and waits for incoming requests. The interface is defined in the
net/httppackage,which is where the ServeHTTP function comes from.
Notice that it receives a
ResponseWriterand a
Request. From just these,you can write out to your response,and get everything about the incoming request. In this case,all we're doing is writing a simple HTML string.
So,type this in your terminal:
$ go run app.go
Your Go program is now waiting for requests. If you set up Nginx.conf like mine,go to
http://go.dev/appand you should see,in all its glory:
That was pretty easy!
原文链接:https://www.f2er.com/go/191261.htmlThat was pretty easy!