Nginx and Go through unix socket

Simple setup example

Our nginx vhost:

upstream ango {
server unix:/tmp/ango.sock;
}

# the nginx server instance
server {
listen 80;
server_name go.dev *.go.dev;
error_log /var/log/nginx/go_error.log;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1; # for keep-alive
proxy_pass http://ango/;
proxy_redirect off;
}
}

The Go app

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net"
	"net/http"
)

var (
	router *mux.Router = mux.NewRouter()
)

func main() {
	// This is the route and an anonymous or first class function to return 

Hello World

router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "

Hello World

") }) // This here is an example of Chuck Han https://groups.google.com/forum/#!topic/golang-nuts/41TPj4PWBI8 // Listen on unix socket /tmp/ango.sock l, err := net.Listen("unix", "/tmp/ango.sock") if err != nil { fmt.Printf("%s\n", err) } else { err := http.Serve(l, router) if err != nil { panic(err) } } }

Beteilige dich an der Unterhaltung

1 Kommentar

Schreib einen Kommentar

Schreibe einen Kommentar zu dalu Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.