小编Bla*_*ter的帖子

如何配置 NGINX 以从 UNIX 套接字提供 ASGI?

我无法通过 docker 容器通过本地主机上 NGINX 上的 Unix 套接字连接在 ASGI + Gunicorn 上运行的应用程序。

假设我在 docker 容器中并运行 NGINX:

/usr/sbin/nginx
Run Code Online (Sandbox Code Playgroud)

我可以打开http://localhost/api/v1/items并从 NGINX 获得 404,这意味着它至少正在运行。

执行正在运行的 docker 服务,我可以使用以下命令启动 Gunicorn:

gunicorn app.main:app --name asgi --workers 3 --user=root --group=root --bind=unix:///tmp/asgi.sock --log-level=debug --log-file=- -k uvicorn.workers.UvicornWorker -c /gunicorn_conf.py
Run Code Online (Sandbox Code Playgroud)

Gunicorn 正确启动,使用另一个 exec,我可以卷曲我绑定的 UNIX 套接字并收到 200 响应。

curl --unix-socket ///tmp/asgi.sock http://localhost/api/v1/items
Run Code Online (Sandbox Code Playgroud)

我认为这意味着我在 NGINX 将流量定向到 http://localhost/api/v1/items 的配置中存在一些差异。

配置文件

daemon off;
user  nginx;
worker_processes 1;
pid        /var/run/nginx.pid;
events {
    worker_connections 1024;
}

http {
  access_log /dev/stdout;
  upstream asgi {
    server unix:/tmp/asgi.sock …
Run Code Online (Sandbox Code Playgroud)

nginx supervisord gunicorn docker asgi

6
推荐指数
1
解决办法
3784
查看次数

标签 统计

asgi ×1

docker ×1

gunicorn ×1

nginx ×1

supervisord ×1