【问题标题】:How to Configure NGINX to Serve ASGI from UNIX Socket?如何配置 NGINX 以从 UNIX 套接字服务 ASGI?
【发布时间】:2019-12-23 06:30:42
【问题描述】:

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

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

/usr/sbin/nginx

我可以打开 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

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

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

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

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))

【问题讨论】:

    标签: docker nginx gunicorn supervisord asgi


    【解决方案1】:

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


    在我的原始帖子中,我的 NGINX 配置在 http 块中没有服务器,我更新了我的日志以写入控制台,但假设文件位置存在,这没什么区别。

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

    我用我最新版本的配置更新了我的帖子,包括 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
    

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

    要自己尝试一下,请参阅我为此 application 制作的 github 存储库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-05
      • 2015-05-05
      • 2015-05-31
      • 2020-05-09
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多