NGINX Unit

Starlette§

要使用 Unit 运行使用 Starlette Web 框架构建的应用

  1. 使用 Python 3.5+ 语言模块安装 Unit

  2. 创建一个虚拟环境来安装 Starlette 的 PIP 包

    $ cd /path/to/app/
    $ python3 --version
          Python 3.Y.Z
    $ python3 -m venv venv
    $ source venv/bin/activate
    $ pip install 'starlette[full]'
    $ deactivate
    

    警告

    使用与步骤 1 中的语言模块匹配的 Python 版本(本例中为 3.Y)创建虚拟环境,且步骤 5 中的应用 类型 必须 解析 为类似匹配的版本;Unit 不会从环境中推断它。

  3. 让我们尝试一个 教程应用 的版本,将其另存为 /path/to/app/asgi.py

    from starlette.applications import Starlette
    from starlette.responses import PlainTextResponse
    from starlette.routing import Route, Mount, WebSocketRoute
    
    
    def homepage(request):
        return PlainTextResponse('Hello, world!')
    
    def user_me(request):
        username = "John Doe"
        return PlainTextResponse('Hello, %s!' % username)
    
    def user(request):
        username = request.path_params['username']
        return PlainTextResponse('Hello, %s!' % username)
    
    async def websocket_endpoint(websocket):
        await websocket.accept()
        await websocket.send_text('Hello, websocket!')
        await websocket.close()
    
    def startup():
        print('Ready to go')
    
    
    routes = [
        Route('/', homepage),
        Route('/user/me', user_me),
        Route('/user/{username}', user),
        WebSocketRoute('/ws', websocket_endpoint)
    ]
    
    app = Starlette(debug=True, routes=routes, on_startup=[startup])
    

    注意

    此示例省略了静态路由,因为 Unit 本身完全 能够 在需要时提供静态文件。

  4. 运行以下命令,以便 Unit 可以访问 应用程序目录

    # chown -R unit:unit /path/to/app/
    

    注意

    unit:unit 用户组对仅适用于 官方包、Docker 映像 和一些 第三方仓库。否则,帐户名称可能有所不同;运行 ps aux | grep unitd 命令以确保。

    有关更多详细信息,包括权限,请参阅 安全检查清单

  5. 接下来,准备 Unit 的 Starlette 配置(对 类型主页路径 使用实际值),添加一个 路由 来提供静态内容

    {
        "listeners": {
            "*:80": {
                "pass": "routes"
            }
        },
    
        "routes": [
            {
                "match": {
                    "uri": "/static/*"
                },
    
                "action": {
                    "share": "/path/to/app$uri"
                }
            },
    
            {
                "action": {
                    "pass": "applications/starlette"
                }
            }
        ],
    
        "applications": {
            "starlette": {
                "type": "python 3.Y",
                "path": "/path/to/app/",
                "home": "/path/to/app/venv/",
                "module": "asgi",
                "callable": "app"
            }
        }
    }
    
  6. 上传更新后的配置。假设上述 JSON 已添加到 config.json

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config/
    

    注意

    控制套接字 路径可能有所不同;运行 unitd -h 或参见 启动和关闭 了解详情。

    成功更新后,您的应用应可在侦听器的 IP 地址和端口上使用

    $ curl http://localhost
    
          Hello, world!
    
    $ curl http://localhost/user/me
    
          Hello, John Doe!
    
    $ wscat -c ws://localhost/ws
    
          Connected (press CTRL+C to quit)
          < Hello, websocket!
          Disconnected (code: 1000, reason: "")