Docker容器中的Daphne + Supervisor无法访问我的应用程序。

3

我正在尝试扩展我的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;

    }
}

1
你最终把你的 supervisor 包放在哪里了?你是把它放在与 Daphne 相同的容器中,还是与 Nginx 相同的容器中,或者是完全独立的容器中? - Timothyjames67
1
@Timothyjames67 我将它放入与 Daphne 相同的容器中,这样它就可以访问 Daphne 的进程。 - MijatTomić
1
@Timothyjames67,Nginx位于自己的容器中,并将请求重定向到其他容器。它与Supervisor和Daphne无关。 - MijatTomić
1个回答

2

好的!我��到了问题所在。 应该是

socket=tcp://localhost:8000

它必须是这样的

socket=tcp://0.0.0.0:8000

这样它就可以在容器外部访问。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接