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

How to Configure NGINX to Serve ASGI from UNIX Socket?

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

前提是我在docker容器和运行NGINX中:

/usr/sbin/nginx

我可以打开 http://localhost/api/v1/items 并从 NGINX 得到一个 404,这意味着它至少是 运行ning。

执行 运行ning 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

Gunicorn 正确启动,并且通过另一个 exec,我可以 curl 我绑定的 UNIX 套接字并收到 200 响应。

curl --unix-socket ///tmp/asgi.sock http://localhost/api/v1/items

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

nginx.conf

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 fail_timeout=0;
  }

  server {
    listen   80;
    server_name localhost;
    client_max_body_size 4G;
    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://asgi;
    }

  }
}

gunicorn_conf.py

import json
import multiprocessing
import os

workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
host = os.getenv("HOST", "unix")
port = os.getenv("PORT", "///tmp/asgi.sock")
bind_env = os.getenv("BIND", None)
use_loglevel = os.getenv("LOG_LEVEL", "info")
if bind_env:
    use_bind = bind_env
else:
    use_bind = f"{host}:{port}"

cores = multiprocessing.cpu_count()
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores
if web_concurrency_str:
    web_concurrency = int(web_concurrency_str)
    assert web_concurrency > 0
else:
    web_concurrency = max(int(default_web_concurrency), 2)

# Gunicorn config variables
loglevel = use_loglevel
workers = web_concurrency
bind = use_bind
keepalive = 120
errorlog = "-"

# For debugging and testing
log_data = {
    "loglevel": loglevel,
    "workers": workers,
    "bind": bind,
    # Additional, non-gunicorn variables
    "workers_per_core": workers_per_core,
    "host": host,
    "port": port,
}
print(json.dumps(log_data))

解决了我的问题并使用更改的文件更新了原始问题。主要问题是为 supervisord 使用 daemon off 以允许 NGINX 和 Gunicorn 运行,并将 NGINX 的服务器配置放在 html 块中。


在我原来的 post 中,我的 NGINX 配置没有在 http 块中包含服务器,我更新了我的日志以写入控制台,尽管假设文件位置存在这没什么区别。

我还通过 Supervisord 启动了 NGINX 和 Gunicorn,这最初并没有说明,因为我觉得这超出了问题的范围。但是,我现在可以使用 Supervisord 启动这两个进程,但我必须 daemon off; 到 NGINX 配置才能让它工作;没有它,进程会说端口已被使用。

我用最新版本的配置更新了 post,包括 supervisor.ini

supervisor.ini

[supervisord]
nodaemon=true

[program:asgi]
command=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
user = root ; User to run as
autostart=true
autorestart=true
stdout_logfile=/dev/stdout ; Where to write log messages
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 ; Set UTF-8 as default encoding

[program:nginx]
command=/usr/sbin/nginx -c /etc/nginx/conf.d/nginx.conf
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
# Graceful stop, see http://nginx.org/en/docs/control.html
stopsignal=QUIT

致运行 主管:/usr/bin/supervisord -c /etc/supervisor.d/supervisord.ini

要亲自尝试,请参阅我为此 application 创建的 github 存储库。