nginx Unit
install
# yum install unit unit-go
start
# systemctl start unit
unit の設定確認
# curl --unix-socket /run/control.unit.sock http://localhost/ { "listeners": {}, "applications": {} }
GOPATH へ src を移動
# cp -a /usr/share/gocode/src/nginx /root/go/src/
アプリ
package main import ( "fmt" "net/http" "unit" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "<h1>Hello NGINX Unit</h1>") }) unit.ListenAndServe(":8000", nil) }
build
# cd /opt/ # export GOPATH=/root/go/ # go build -o server main.go
設定を投げつける
設定ファイル
{ "applications": { "go_server": { "type": "go", "user": "nobody", "executable": "/opt/server" } }, "listeners": { "*:8000": { "application": "go_server" } } }
curl
$ curl -X PUT -d @./start.json --unix-socket /run/control.unit.sock http://localhost/ # curl -X PUT -d @./start.json --unix-socket /run/control.unit.sock http://localhost/ { "success": "Reconfiguration done." }
確認
# curl --unix-socket /run/control.unit.sock http://localhost/ { "applications": { "go_server": { "type": "go", "user": "nobody", "executable": "/opt/server" } }, "listeners": { "*:8000": { "application": "go_server" } } }
実行
# curl localhost:8000 <h1>Hello NGINX Unit</h1>
nginx との連携
upstream の設定
# cat /etc/nginx/conf.d/go.conf upstream unit_backend { server 127.0.0.1:8000; }
サンプル
location /go { proxy_pass http://unit_backend; proxy_set_header Host $host; }