【问题标题】:How can i run Django channels on production?如何在生产环境中运行 Django 频道?
【发布时间】:2020-07-28 04:02:55
【问题描述】:

我将一个简单的 Django 应用部署到 VPS,这是我的环境:

virtualenv
gunicorn
nginx
systemd

一切正常,我可以看到我的模板正在加载。我还添加了一个小的 Django Channels 功能,但那部分不起作用;所以虽然我可以在 WSGI 上毫无问题地使用它,但如果我尝试联系消费者,我会收到错误消息。所以我的问题是:如何在生产环境中运行 Channels?

这是我目前所做的:

/etc/nginx/sites-available/myproject

server {
        listen 80;
        server_name 54.39.20.155;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /WaitingRoomVenv/WaitingRoom/WaitingRoom/static;
        }

        location / {
            include proxy_params;
            proxy_pass http://unix:/WaitingRoomVenv/WaitingRoomEnv.sock;
        }
    }

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/WaitingRoomVenv/WaitingRoom
ExecStart=/WaitingRoomVenv/WaitingRoomEnv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/WaitingRoomVenv/WaitingRoomEnv.sock WR.wsgi:application

[Install]
WantedBy=multi-user.target

启动 gunicorn:sudo systemctl start gunicorn 启动nginx:sudo systemctl restart nginx

【问题讨论】:

    标签: python django nginx gunicorn django-channels


    【解决方案1】:

    要访问频道,您需要通过像 daphne(随频道一起提供)这样的 ASGI 服务器而不是您正在使用的 WSGI geunicorn 运行,请参阅:

    https://channels.readthedocs.io/en/latest/deploying.html

    要启动服务器 daphne -p 8001 myproject.asgi:application 然后在你的 nginx 中你需要代理传递到端口 8001

    【讨论】:

    • 是的,但我不明白的是:我只需要编辑我当前的 nginx 配置(我在这里粘贴的第一个代码块)还是需要创建一个新配置?还有,你提到的daphne命令,不应该是守护进程什么的吗?
    • 这样您就可以让 daphne 为您的整个应用程序提供服务器,也就是普通的 HTTP 视图和 WS 消费者。
    • daphne command you mentioned, shouldn't it be daemonized or something?,如果你直接运行它,是的,就像你的 gunicorn 一样。但我建议(如果你能的话)将这些东西部署为 docker 容器。
    • 如果您不想通过 daphne 运行整个服务器,则将基于 rout 的路径为您的 ws 路由添加到您的 nginx 中,以便将这些请求路由到 daphne。
    • 好的,所以我不需要创建一个新的 nginx 文件,只需编辑一个新的?在这种情况下,我不明白我应该如何编辑当前的。是的,我打算以后用docker,这只是一个测试,学习一些部署实践
    猜你喜欢
    • 2011-03-20
    • 2021-07-08
    • 2022-11-22
    • 2019-09-26
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多