【问题标题】:django channel issue during deployment部署期间的django频道问题
【发布时间】:2022-01-06 08:22:09
【问题描述】:

我正在使用 Django Channels 并使用 NGINX+Gunicorn+Uvicorn 进行部署,并遵循 Digital Ocean 的教程(即https://www.digitalocean.com/community/tutorials/how-to-set-up-an-asgi-django-app-with-postgres-nginx-and-uvicorn-on-ubuntu-20-04

当我尝试使用以下命令在不使用 NGINX 的情况下运行站点 @www.myproject.com:8000 时,没有发现任何问题:

gunicorn --bind 0.0.0.0:8000 myproject.asgi -w 4 -k uvicorn.workers.UvicornWorker

但是,当我使用 NGINX 进行部署时,除了使用 django 频道的聊天功能页面之外,大多数网站功能都可以。

websocket 不知何故无法连接,如浏览器错误所示:

WebSocket connection to 'ws://myproject.com/ws/chat/ADVPKG/' failed: Error during WebSocket handshake: Unexpected response code: 500 

查看服务器中的错误日志,我看到了:

2022/01/06 08:13:02 [alert] 16624#16624: *8421 768 worker_connections are not enough while connecting to upstream, client: 127.0.0.1, server: www.myproject.com, request: "GET /ws/chat/ADVPKG/ HTTP/1.1", upstream: "http://127.0.0.1:80/ws/chat/ADVPKG/", host: "myproject.com"

下面是我的代码: asgi.py

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django
django.setup()

from django.core.asgi import get_asgi_application
django_asgi_app = get_asgi_application()


from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import chat.routing

application = ProtocolTypeRouter({
  "http": django_asgi_app,
  "websocket": AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})

/etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name www.myproject.com
                myproject.com
                ""
                1X.XXX.XX.XXX
                ;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/stochie;
    }

    location /ws/ {
        proxy_pass http://0.0.0.0;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection “upgrade”;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

基本上:

  1. 我很确定问题出在 nginx 配置上,因为没有 nginx,每个功能都可以正常工作
  2. 我严重怀疑 'proxy_pass http://0.0.0.0;'可能是错误的,根据一份论坛调查问卷,这是由于无限循环作为代理通行证指向自身。到目前为止,根据其他论坛问卷调查,我尝试了 'localhost'、'localhost:8000'、http://django、http://uvicorn 但无法解决问题。

【问题讨论】:

  • 这个问题你解决了吗,我现在也面临同样的问题?

标签: nginx gunicorn django-channels uvicorn asgi


【解决方案1】:

我认为问题出在你asgi.py

从您的asgi.py 中删除路由并将此代码放入您的asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

django.setup()
application = get_default_application()

然后在下面创建类似这样的routings.py 并将myproject.routings.application 放入您的settings.py

application = ProtocolTypeRouter({
    'websocket': OriginValidator(
        TokenAuthMiddleware(
            URLRouter([
                url(r"^ws/courier/$", CourierConsumer.as_asgi()),
                url(r"^.*$", NoRouteFoundConsumer.as_asgi()),
            ])
        ),
        ['*'],
    ),
})

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 2019-02-01
    • 2021-07-30
    • 2019-08-18
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多