小编Utk*_*aps的帖子

无法使用 Nginx 和 Uvicorn 连接到 websocket

我用 Django、Uvicorn、Nginx 和 Redis 构建了一个 docker 容器,并且正在使用 django-channels,但是当我运行它时,它说它无法连接到 websocket,这可以在浏览器控制台中看到:

WebSocket connection to 'ws://127.0.0.1:8080/ws/notifications/' failed

当我使用 Django 的 runserver 命令进行开发时,它工作正常,但当我包含 Nginx 和 Uvicorn 时,它就崩溃了。

入口点.sh:

gunicorn roomway.asgi:application --forwarded-allow-ips='*' --bind 0.0.0.0:8000 -k uvicorn.workers.UvicornWorker
Run Code Online (Sandbox Code Playgroud)

Nginx 配置:

upstream django {
    server app:8000;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 8080;

    location /static {
        alias /vol/static;
    }

    location /ws/ {
        proxy_pass http://0.0.0.0:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location / {
        proxy_pass http://django;
        proxy_set_header Host $http_host; …
Run Code Online (Sandbox Code Playgroud)

django nginx websocket docker uvicorn

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

django ×1

docker ×1

nginx ×1

uvicorn ×1

websocket ×1