1.簡介
Nginx Unit是一個面向微服務架構的、依托Nginx、支持多語言的動態(tài)Web應用服務器。
在2018年6月7日發(fā)布了1.2版本。
官網(wǎng):https://www.nginx.com/products/nginx-unit/
項目地址:https://github.com/nginx/unit
2.功能
- 可使用 RESTful JSON API 動態(tài)配置服務器
- 可同時運行多語言及多版本的應用
- 動態(tài)語言的進程管理功能(開發(fā)中)
- TLS 支持(開發(fā)中)
- TCP, HTTP, HTTPS, HTTP/2 路由和代理(開發(fā)中)
3.支持語言
- Python
- PHP
- Go
- Perl
- Ruby
- JavaScript/Node.js (開發(fā)中)
- Java (開發(fā)中)
4.環(huán)境搭建
OS:Ubuntu 18.04
4.1 下載密鑰并將其添加到apt的keyring
$ wget https://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
該密鑰用于驗證Nginx公司簽發(fā)的倉庫軟件包,以消除安裝過程中的告警。
4.2 新增軟件源
創(chuàng)建文件:/etc/apt/sources.list.d/unit.list,并
添加內容:
deb https://packages.nginx.org/unit/ubuntu/ bionic unit
deb-src https://packages.nginx.org/unit/ubuntu/ bionic unit

4.3 安裝Unit基礎包
$ sudo apt-get update
$ sudo apt-get install unit
4.4 安裝模塊包
根據(jù)需要添加:
$ sudo apt-get install unit-python3.6 unit-go1.9
當前1.2版本支持模塊有:
unit-python2.7
unit-python3.6
unit-go1.9
unit-go1.10
unit-perl
unit-php
unit-ruby
安裝輸出信息:
The Go 1.9 module for NGINX Unit has been installed.
To check out the sample app, run these commands:
GOPATH=/usr/share/gocode /usr/lib/go-1.9/bin/go build -o /tmp/go1.9-app /usr/share/doc/unit-go1.9/examples/go-app/let-my-people.go
sudo service unit restart
sudo service unit loadconfig /usr/share/doc/unit-go1.9/examples/unit.config
curl http://localhost:8500/
Online documentation is available at https://unit.nginx.org
----------------------------------------------------------------------
Unpacking unit-go1.9 (1.2-1~bionic) ...
Selecting previously unselected package unit-python3.6.
Preparing to unpack .../7-unit-python3.6_1.2-1~bionic_amd64.deb ...
----------------------------------------------------------------------
The Python 3.6 module for NGINX Unit has been installed.
To check out the sample app, run these commands:
sudo service unit restart
sudo service unit loadconfig /usr/share/doc/unit-python3.6/examples/unit.config
curl http://localhost:8400/
Online documentation is available at https://unit.nginx.org
----------------------------------------------------------------------
Unpacking unit-python3.6 (1.2-1~bionic) ...
Setting up golang-1.9-race-detector-runtime (0.0+svn285455-0ubuntu1) ...
Setting up pkg-config (0.29.1-0ubuntu2) ...
Setting up golang-1.9-src (1.9.4-1ubuntu1) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up unit-python3.6 (1.2-1~bionic) ...
Setting up golang-1.9-go (1.9.4-1ubuntu1) ...
Setting up golang-1.9-doc (1.9.4-1ubuntu1) ...
Setting up golang-1.9 (1.9.4-1ubuntu1) ...
Setting up unit-go1.9 (1.2-1~bionic) ...
5.開發(fā)步驟
5.1 啟動unit服務
$ sudo unitd --control 127.0.0.1:8443
為了便于使用RESTful JSON API動態(tài)配置服務器,配置--control指定IP和端口,默認采用:
unix:/var/run/control.unit.sock
查看進程是否成功啟動:

調用返回如下信息,說明啟動成功:

5.2 服務接口編寫
- Go
文件名:unit.go
package main
import (
"fmt"
"net/http"
"nginx/unit"
)
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "Go ngnix unit")
}
func main() {
http.HandleFunc("/", index)
unit.ListenAndServe("8000", nil)
}
編譯:
$ GOPATH=/usr/share/gocode go build -o go-app unit.go

- python
unit.py
import sys
def application(environ, start_response):
body = sys.version.encode("utf-8")
status = "200 OK"
headers = [("Content-type", "text/plain")]
start_response(status, headers)
return [body]
5.3 動態(tài)全量添加配置
$ cat go.json
{
"applications": {
"example_go": {
"type": "go",
"user": "nobody",
"executable": "/home/jasonruan/Software/Ngnix/Unit/example/go/go-app"
}
},
"listeners": {
"*:8500": {
"application": "unit_go"
}
}
}
- 全量配置動態(tài)加載:
$ curl -X PUT -d@go.json 127.0.0.1:8443
{
"success": "Reconfiguration done."
}
-
查看配置,已加載成功:
-
測試接口,返回預期信息:
5.4 動態(tài)調整配置
- 編寫python服務接口的配置,進行增量追加
配置:
$ cat unit_python.json
{
"type": "python 3.6",
"user": "nobody",
"processes": 2,
"path": "/home/jasonruan/Software/Ngnix/Unit/example/python",
"module": "unit"
}
- 追加配置:
$ curl -X PUT -d@unit_python.json '127.0.0.1:8443/applications/unit_python'
{
"success": "Reconfiguration done."
}
- 查看配置情況:
$ http 127.0.0.1:8443
HTTP/1.1 200 OK
Connection: close
Content-Length: 421
Content-Type: application/json
Date: Wed, 13 Jun 2018 01:22:42 GMT
Server: Unit/1.2
{
"applications": {
"unit_go": {
"executable": "/home/jasonruan/Software/Ngnix/Unit/example/go/go-app",
"type": "go",
"user": "nobody"
},
"unit_python": {
"module": "unit",
"path": "/home/jasonruan/Software/Ngnix/Unit/example/python",
"processes": 2,
"type": "python 3.6",
"user": "nobody"
}
},
"listeners": {
"*:8500": {
"application": "unit_go"
}
}
}
- 將listeners動態(tài)改為unit_python:
$ curl -X PUT -d '"unit_python"' '127.0.0.1:8443/listeners/*:8500/application'
{
"success": "Reconfiguration done."
}

-
測試:
符合預期,返回內容是python api提供服務內容。
追加Go的listener配置:
$ curl -X PUT -d@unit_go.json '127.0.0.1:8443/listeners/*:8600'

-
測試:
Go和Python服務均能正常訪問,符合預期。
5.5 動態(tài)刪除配置
$ curl -X DELETE '127.0.0.1:8443/listeners/*:8600'
{
"success": "Reconfiguration done."
}
$ curl -X DELETE '127.0.0.1:8443/applications/unit_go'
{
"success": "Reconfiguration done."
}
注:需要刪除listeners后方能刪除applications配置。

6.后記
本文只是一篇初淺的入門文章,重點介紹了環(huán)境搭建以及動態(tài)配置服務器,與Nginx整合及安全性等方面后續(xù)進行補充。
7.參考
http://unit.nginx.org/?_ga=2.114330055.1787926827.1528855190-288714655.1525766596



