nginx/unit
GitHub: nginx/unit
NGINX Unit 是一款开源通用 Web 应用服务器,通过统一的 RESTful API 原生运行八种编程语言的应用代码并提供静态资源服务。
Stars: 5553 | Forks: 372
# NGINX Unit
[](https://www.repostatus.org/#unsupported)
## 注意:此仓库已被归档。此仓库可能不会再有进一步的开发,安全漏洞可能无法得到解决。
## 该仓库可以在其当前许可证下被克隆和使用。
## 通用 Web 应用服务器

NGINX Unit 是一个轻量级且多功能的开源服务器,它具有
两项主要功能:
- 提供静态媒体资源服务,
- 运行八种语言编写的应用程序代码。
Unit 将现代应用技术栈中的多个层级整合为一个强大且
连贯的解决方案,重点关注性能、低延迟和可扩展性。它
旨在作为任何 Web 架构的通用构建块,无论其复杂程度如何,
从企业级部署到您的个人主页都适用。
其原生的 [RESTful JSON API](#openapi-specification) 支持零中断的动态
更新和灵活配置,同时其开箱即用的生产力能够可靠地
扩展以应对生产级工作负载。我们
通过包含多个进程的复杂、异步、多线程架构
实现了这一点,在确保安全性和鲁棒性的同时,
充分利用当今的计算平台。
## 安装
### macOS
运行以下命令来同时安装 `unitd`(Unit 守护进程)和
`unitctl`(控制工具)。
```
$ brew install nginx/unit/unit
```
有关详细信息和可用的语言包,请参阅
[文档](https://unit.nginx.org/installation/#homebrew)。
### Docker
```
$ docker pull unit:
$ mkdir /tmp/unit-control # customize as needed.
$ docker run -d \
--mount type=bind,src=/tmp/unit-control,dst=/var/run \
--mount type=bind,src=.,dst=/www \
--network host \
unit
```
有关镜像标签的说明,请参阅
[文档](https://unit.nginx.org/installation/#docker-images)。
警告:最新的镜像标签可能不提供对特定语言
模块的支持,在拉取镜像*之前*,请务必通过上方链接查看可用的镜像标签。
您当前的工作目录现在将被挂载到 Unit 镜像的 `/www` 目录。
假设没有进行进一步的自定义,您可以通过
`/tmp/unit-control/control.unit.sock` 访问其套接字。
### Debian, Ubuntu, Amazon Linux, Fedora, Red Hat
此辅助脚本会为系统配置正确的软件源。
```
$ wget https://raw.githubusercontent.com/nginx/unit/master/tools/setup-unit && chmod +x setup-unit
# ./setup-unit repo-config
```
Debian 衍生版:
```
# apt install unit
```
Fedora 衍生版:
```
# yum install unit
```
有关详细信息和可用的语言包,请参阅
[文档](https://unit.nginx.org/installation/#official-packages)。
## `unitctl` 入门
[`unitctl`](tools/README.md) 通过一个易于使用的命令行界面
简化了 NGINX Unit 进程的管理。要开始使用 `unitctl`,
请从
[GitHub 官方发布页面](https://github.com/nginx/unit/releases)
或 [Homebrew](#macos) 下载。
### 安装
从 [NGINX Unit 发布页面](https://github.com/nginx/unit/releases/) 为您的系统下载
适用的 `unitctl` 二进制文件。
```
$ tar xzvf unitctl-master-x86_64-unknown-linux-gnu.tar.gz
# mv unitctl /usr/local/bin/
```
## 使用 Docker 启动 Unit
如果您已在计算机上[安装了 Docker](https://docs.docker.com/engine/install/),
那么您可以轻松地将 [Unit 官方 Docker 镜像之一](https://hub.docker.com/_/unit)
与您的应用程序一起启动。
以下是使用 `unit:python` Docker 镜像的示例:
```
$ unitctl instances new 127.0.0.1:8001 /path/to/app 'unit:python'
```
`/path/to/app` 将挂载到 Docker 文件系统中的 `/www`。
将其保存为 `/path/to/app/wsgi.py`:
```
def application(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
return (b"Hello, Python on Unit!")
```
然后,您可以交互式地编辑当前的活动配置:
```
$ unitctl edit
```
```
{
"listeners": {
"*:8000": {
// Point listener to new application
"pass": "applications/python"
}
},
// Add an application definition
"applications": {
"python": {
"type": "python",
"path": "/www/",
"module": "wsgi"
}
}
}
```
有效的配置将在保存并关闭后被应用。
```
$ curl localhost:8000
Hello, Python on Unit!
```
更多的 Python 配置示例可以在
[Unit 文档](https://unit.nginx.org/howto/samples/#python)中找到。
## 使用 PHP 和 curl 实现 Hello World
Unit 支持[多种语言](https://unit.nginx.org/howto/samples/)运行
应用。让我们通过 `curl` 来探索在 Unit 上配置一个简单的 PHP 应用。
假设您将一个 PHP 脚本保存为 `/www/helloworld/index.php`:
```
```
要在已安装 `unit-php` 模块的 Unit 上运行它,请首先设置一个
应用对象。让我们将第一个配置片段存储在一个名为
`config.json` 的文件中:
```
{
"helloworld": {
"type": "php",
"root": "/www/helloworld/"
}
}
```
将其保存为文件并不是必须的,但在处理较大的对象时会非常方便。
现在,将其 `PUT` 到 Unit 控制 API 的 `/config/applications` 部分,
该 API 通常默认通过 Unix 域套接字可用:
```
# curl -X PUT --data-binary @config.json --unix-socket \
/path/to/control.unit.sock http://localhost/config/applications
```
```
{
"success": "Reconfiguration done."
}
```
接下来,在 API 的 `/config/listeners` 部分的监听器对象中引用该应用。
这一次,我们直接从命令行传递配置片段:
```
# curl -X PUT -d '{"127.0.0.1:8080": {"pass": "applications/helloworld"}}' \
--unix-socket /path/to/control.unit.sock http://localhost/config/listeners
```
```
{
"success": "Reconfiguration done."
}
```
现在,Unit 开始在指定的 IP 和端口上接收请求,并将其传递给
应用程序进程。您的应用运行成功了!
```
$ curl 127.0.0.1:8080
Hello, PHP on Unit!
```
最后,查询控制 API 的整个 `/config` 部分:
```
# curl --unix-socket /path/to/control.unit.sock http://localhost/config/
```
Unit 的输出应包含这两个片段,并且组织得井井有条:
```
{
"listeners": {
"127.0.0.1:8080": {
"pass": "applications/helloworld"
}
},
"applications": {
"helloworld": {
"type": "php",
"root": "/www/helloworld/"
}
}
}
```
## WebAssembly
Unit 支持运行 WebAssembly Components (WASI 0.2)。
有关更多信息,请参阅
[Unit 配置文档](https://unit.nginx.org/configuration/#configuration-wasm)。
## OpenAPI 规范
我们的 [OpenAPI 规范](docs/unit-openapi.yaml) 旨在简化
NGINX Unit 部署的配置和集成,并提供有关控制 API 的
权威知识来源。
## 社区
- 开始提问和分享想法的首选之地是
[GitHub Discussions](https://github.com/nginx/unit/discussions)。
- 我们的 [GitHub issues 页面](https://github.com/nginx/unit/issues) 为
您提供了按自己的节奏进行更技术性讨论的空间。
- GitHub 上的[项目路线图](https://github.com/orgs/nginx/projects/1)
让我们了解当前的工作进展和未来的计划。
- 我们的[官方网站](https://unit.nginx.org/) 可能会提供
其他地方不易找到的答案。
- 通过做贡献来参与该项目!详情请参阅
[贡献指南](CONTRIBUTING.md)。
- 要直接联系团队,请订阅
[邮件列表](https://mailman.nginx.org/mailman/listinfo/unit)。
- 对于安全问题,请[给我们发送电子邮件](mailto:security-alert@nginx.org),
在主题中注明 NGINX Unit,并遵循 [CVSS
v3.1](https://www.first.org/cvss/v3.1/specification-document) 规范。
标签:AI工具, JS文件枚举, MITM代理, NGINX, Web服务器, 动态配置, 多语言运行时, 安全监控, 客户端加密, 幻觉检测, 应用服务器, 日志审计, 请求拦截, 运维, 逆向工具