Bla*_*ter 6 nginx supervisord gunicorn docker 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 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
gunicorn_conf.py
/usr/sbin/nginx
Run Code Online (Sandbox Code Playgroud)
解决了我的问题并用更改的文件更新了原始问题。主要问题是使用 Supervisord 的守护进程关闭来允许 NGINX 和 Gunicorn 运行,并将 NGINX 的服务器配置放置在 html 块中。
在我原来的帖子中,我的 NGINX 配置在 http 块内没有服务器,并且我更新了日志以写入控制台,尽管假设文件位置存在,这没什么区别。
我还通过 Supervisord 启动 NGINX 和 Gunicorn,这最初并未说明,因为我觉得这超出了问题的范围。不过,我现在可以使用 Supervisord 启动这两个进程,但我必须daemon off;
通过 NGINX 配置才能使其正常工作;如果没有它,该进程会说端口已在使用中。
我用最新版本的配置更新了我的帖子,包括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
Run Code Online (Sandbox Code Playgroud)
运行supervisord:/usr/bin/supervisord -c /etc/supervisor.d/supervisord.ini
要亲自尝试,请参阅我为此应用程序制作的 github 存储库。
归档时间: |
|
查看次数: |
3784 次 |
最近记录: |