加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用golang fastcgi与nginx配合写web

发布时间:2020-12-16 18:59:03 所属栏目:大数据 来源:网络整理
导读: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

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!
原文链接: http://mwholt.blogspot.com/2013/05/writing-go-golang-web-app-with-nginx.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读