Writing web with Golang fastcgi and Nginx

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Configure nginx/fastcgi

This was actually pretty easy. I assume you already has some experience configuring nginx.conf. (each install seemingly have different defaults as to the Conf file's location, and contents, so I-won ' t go over it here. M INE is In/usr/local/etc/nginx.)
I assume too you ' ve configured PHP with FastCGI before. If not, the still understand what ' s happening.
All of the certain requests, or maybe all of the them if you wish, to FastCGI on a certain port. Our Go program has a FastCGI handler listening on that same port. If you need a reference, my entire server {...}Block looks like this:
server { Listen; 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. If 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 isn't the default 9000, but is 9001. I did this because I already has php-fpm listening on port 9000. My Go app is listen on 9001.
Further notice that the Fastcgi_passis in a Location ~ {...}Block such that any page under /appOf Go.dev'll is redirected to my Go program. 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 being 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 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 (" }
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 are our FastCGI PA Ss-thru Port from the Nginx config. Then we do a new fastcgiserver and serve requests that pops into the that port. The FCGI package has a function:serve, which blocks and waits for incoming requests. The interface is defined in the net/httpPackage, which are 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 are writing a simple HTML string.
So, type this in your terminal:
$ go Run app.go
Your Go program was now waiting for requests. If you set up nginx.conf like mine, go to Http://go.dev/appAnd you should see, with all its glory:


That was pretty easy!

Original link: http://mwholt.blogspot.com/2013/05/writing-go-golang-web-app-with-nginx.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.