我正在尝试扩展我的Django应用程序,该应用程序在Docker容器中使用Daphne服务器,并使用Supervisor进行管理,因为Daphne没有工作进程。我在互联网上读到应该这样做,但我没有找到任何概念解释,而且文档非常晦涩。
我设法在容器内运行所有内容,日志也没问题。我最初使用多个容器在没有supervisord的情况下运行我的应用程序,它可以正常工作。也就是说,由于冗余,我在多个容器中托管了相同应用程序的多个实例。然后我读到可以使用supervisor在容器内运行应用程序的多个进程。因此,我成功地使用supervisord和daphne在容器内运行应用程序,我得到了应用程序正在运行的日志,但当我只有每个容器一个Daphne进程而没有supervisord时,我无法像以前那样通过浏览器访问它。
更新:
当我使用curl localhost: 8000
时,我甚至可以在容器内部对我的应用程序进行curl,但我不能通过容器的IP地址在容器内外都进行curl。这意味着尽管在docker-compose文件中公开了容器的端口,但其在容器外部不可见。
我得到的结果:
502 Bad Gateway
nginx/1.18.0
我的supervisord配置文件如下:
[supervisord]
nodaemon=true
[supervisorctl]
[fcgi-program:asgi]
User=root
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000
# Directory where your site's project files are located
directory=/app
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command= /usr/local/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --endpoint fd:fileno=0 --access-log - --proxy-headers WBT.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=4
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=/home/appuser/supervisor_log.log
redirect_stderr=true
我不知道 NGINX 为什么会抛出502错误。
这个配置在我引入 supervisor 之前都是工作正常的。
我的 Nginx 也位于自己的 Docker 容器内。
upstream django_daphne{
hash $remote_addr consistent;
server django_daphne_1:8000;
server django_daphne_2:8000;
server django_daphne_3:8000;
}
server {
server_name xxx.yyy.zzz.khmm;
listen 80;
client_max_body_size 64M;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
proxy_pass http://django_daphne;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#Websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /api/ {
proxy_pass http://api_app:8888;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}