用golang fastcgi與nginx配合寫web

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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_name is  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_pass port 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_pass is in a  location ~ { ... } block such that any page under  /app of  go.dev will 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 package has one function: Serve, which blocks and waits for incoming requests. The interface is defined in the net/http package, which is where the ServeHTTP function comes from.
Notice that it receives a  ResponseWriter and 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/app and 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.