I am unable to connect an application running on ASGI + Gunicorn through a Unix socket on NGINX on localhost via a docker container.
Provided that I am in the docker container and run NGINX:
/usr/sbin/nginx
I can open http://localhost/api/v1/items
and get a 404 from NGINX, which means it is at least running.
Exec-ing the running docker service, I can start Gunicorn with the following 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
Gunicorn starts correctly, and with another exec, I can curl the UNIX socket that I have bound to as such and receive a 200 response.
curl --unix-socket ///tmp/asgi.sock http://localhost/api/v1/items
I think this means that I have some discrepancy in the configuration of NGINX directing traffic to 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))