# Unit設定
[toc]
###### tags: `unit` `nginx`
### 1. Unit是一種基於微服務的框架
![NGINX/UNIT](https://i.imgur.com/tncUX9o.png)
NGINX Unit可以用在多種場景中,Unit是一種基於微服務的框架,每個服務一個Unit,每個Unit 可使用不同的程式語言,現在可支援的有
* Go 1.6 or later
* Java 8 or later
* Node.js 8.11 or later
* PHP 5, 7
* Perl 5.12 or later
* Python 2.6, 2.7, 3.x
* Ruby 2.0 or later
最後在由Nginx 整合所有的Unit,保有Nginx的效能與Unit的彈性。
### 2. 安裝Nginx-Unit
安裝Nginx-Unit的方法在Nginx-Unit的官方網站中說明得非常清楚。
https://unit.nginx.org/installation/
可以直接安裝binary檔,也能自行compiler,個人建議直接裝編譯好的binary檔就好。
以centos8為例,新增一個設定檔 nano /etc/yum.repos.d/unit.repo
內容如下
```
[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
然後執行安裝指令
yum -y install unit
yum -y install unit-devel unit-go unit-jsc8 unit-jsc11 unit-perl unit-php unit-python27 unit-python36
安裝好後
systemctl enable unit
systemctl start unit
```
ubuntu可參考 unit.nginx.org中的方法
[https://unit.nginx.org/installation/#ubuntu-2204](https://unit.nginx.org/installation/#ubuntu-2204)
這樣就裝好了!下一篇來跑個 Hello World 第一個微服務吧!
### 3. Unit的指令
Unit 的配置基於 JSON,可透過RESTful控制API ,並且由HTTP進行管理,控制API提供了四個主要選項
| 選項 | 描述 |
| --- | --- |
|/certificates| 負責TLS/SSL證書管理|
|/config |設定與查詢一般配置管理|
|/control |查詢應用程序重啟|
|/status |查詢使用情況與統計資料|
第一步,我們將 Unit 配置為提供靜態網頁。假設您將其保存為/www/data/index.html
```htmlembedded=
<!DOCTYPE html>
<html>
<head>
<title>Welcome to NGINX Unit!</title>
</head>
<body>
<h1>Welcome to NGINX Unit!</h1>
<p>If you see this page, the NGINX Unit web server is successfully
installed and working. Further configuration is required.
</p>
<p>For online documentation and support, please refer to
<a href="https://unit.nginx.org/">unit.nginx.org</a>.<br/>
</p>
<p><em>Thank you for using NGINX Unit.</em></p>
</body>
</html>
```
* /config 的使用方法
Unit 需要監聽一個埠,該埠將傳入請求路由到一個share操作。
“listeners” 監聽 ip:port pass 到 “routes”
“routes” 路由動作action share “/www/data$uri”
其中 $uri 為相對路徑
```jsonld=
{
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"share": "/www/data$uri"
}
}
]
}
```
將其保存並上傳為config.json,配置 Unit,PUT請將此config通過控制命令PUT到該部分。
```
[centos]
curl -X PUT --data-binary @config.json --unix-socket /var/run/unit/control.sock http://localhost/config
[ubuntu]
curl -X PUT --data-binary @config.json --unix-socket /var/run/control.unit.sock http://localhost/config
```
若出現下列訊息表示設定成功,這裡要注意--unix-socket後面的/var/run/unit/control.sock會隨著不同的OS位置不同,請務必注意!
```
{
"success": "Reconfiguration done."
}
```
可用瀏覽器看看是否成功!
* 來觀看現在 unit 的相關設定
用指令
```[centos]curl --unix-socket /var/run/unit/control.sock http://localhost/```
```[ubuntu]curl --unix-socket /var/run/control.unit.sock http://localhost/```
來觀看現在 unit 的相關設定
```jsonld=
{
"certificates": {},
"config": {
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"share": "/home/kevin/html$uri"
}
}
]
},
"status": {
"connections": {
"accepted": 6,
"active": 0,
"idle": 0,
"closed": 6
},
"requests": {
"total": 8
},
"applications": {}
}
}
```
出現了 certificates config status 三個控制指令!
* /control 的使用方法
control指令用來重啟unit中的微服務
**/control指令命令applications裡的app_name要求重啟**
```bash=
curl -X GET --unix-socket /path/to/control.unit.sock http://localhost/control/applications/app_name/restart
```
例如:
```bash=
[centos]
curl -X GET --unix-socket /var/run/unit/control.sock http://localhost/control/applications/python/restart
[ubuntu]
curl -X GET --unix-socket /var/run/control.unit.sock http://localhost/control/applications/python/restart
```
執行下列指令
**看unit 的設定**
[centos]
curl --unix-socket /var/run/unit/control.sock http://localhost/config
[ubuntu]
curl --unix-socket /var/run/control.unit.sock http://localhost/config
### 4. 配置管理
Unit 的配置是基於 JSON 的,通過control socket訪問,並且完全可以通過 HTTP 進行管理
要處理部分配置,請通過 HTTP 查詢控制套接字;您對 API 的請求的 URI 路徑段必須是其JSON成員的名稱或其JSON元素的索引。
您可以使用以下 HTTP 方法操作 API:
方法 動作
GET Returns the entity at the request URI as a JSON value in the HTTP response body.
POST Updates the array at the request URI, appending the JSON value from the HTTP request body.
PUT Replaces the entity at the request URI and returns status message in the HTTP response body.
DELETE Deletes the entity at the request URI and returns status message in the HTTP response body.
在 centOS 或 Amalinux 中執行下列指令,可看到 unit 的設定
curl --unix-socket /var/run/unit/control.sock http://localhost/config
新增設定
curl -X PUT -d '{ "pass": "routes" }' --unix-socket /var/run/unit/control.sock http://localhost/config/listeners/*:80
刪除剛剛的設定
curl -X DELETE -d '{ "pass": "routes" }' --unix-socket /var/run/unit/control.sock http://localhost/config/listeners/*:80
我們透過這個例子可看出,unit可在不重啟的情況下更改unit的設定。
以下為 unit 的一些設定例子
(A)
```jsonld=
config2.json
{
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"uri": "/static/*"
},
"action": {
"share": "/home/kevin$uri"
}
},
{
"action": {
"share": "/home/kevin/html$uri"
}
}
]
}
```
/home/kevin/html
/home/kevin/static/
Set
curl -X PUT --data-binary @config2.json --unix-socket /var/run/unit/control.sock http://localhost/config
READ Config
curl --unix-socket /var/run/unit/control.sock http://localhost/config
這個例子會將
http://host/static/uri -> /home/kevin/static/uri
http://host/ uri -> /home/kevin/html/uri
(B)
```jsonld=
config3.json
{
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"uri": "/static/*"
},
"action": {
"share": "/home/kevin$uri"
}
},
{
"action": {
"pass": "applications/pythonapp"
}
}
],
"applications": {
"pythonapp": {
"type": "python",
"path": "/home/kevin/project/",
"home": "/home/kevin/env/",
"module": "app",
"user": "kevin"
}
}
}
```
```python=
/home/kevin/project/app.py
def application(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/html; charset=utf8')]
start_response(status, headers)
path_info = environ['PATH_INFO']
query_string = environ['QUERY_STRING']
return [b'<h1>Hello, World!',b'PATH_INFO:',path_info.encode(),b'QUERY_STRING:',query_string.encode(),b'</h1>']
```
這部分與python內建的wsgiserver不同,直接用 unit 當這個 micro service的 server。
/home/kevin/project/
/home/kevin/static/
Set
```bash=
curl -X PUT --data-binary @config3.json --unix-socket /var/run/unit/control.sock http://localhost/config
[READ Config]
curl --unix-socket /var/run/unit/control.sock http://localhost/config
```
```
To check out the sample app, run these commands:
sudo service unit restart
cd /usr/share/doc/unit-python3.10/examples
sudo curl -X PUT --data-binary @unit.config --unix-socket /var/run/control.unit.sock http://localhost/config
curl http://localhost:8400/
example:
{
"applications": {
"example_python": {
"type": "python 3.10",
"processes": 2,
"path": "/usr/share/doc/unit-python3.10/examples/python-app",
"module": "wsgi"
}
},
"listeners": {
"*:8400": {
"pass": "applications/example_python"
}
}
}
```