django-channels:找不到页面,连接失败

django-channels: page not found, connection failed

我正在尝试设置 django-channels

我按照说明做了所有事情,但是当我尝试连接时,我得到一个 404 error - page not found

我从本地主机向开发服务器发出请求。

config/base.py

INSTALLED_APPS = [
    "grappelli",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",

    "adminsortable2",
    "channels",
    "corsheaders",
    "django_filters",
    "django_multilanguage_content",
    "django_otp",
    "django_otp.plugins.otp_totp",
    "drf_yasg",
    "notifications",
    "rest_framework",
    "rest_registration",
    "storages",

    "vv.announcements.apps.AnnouncementsConfig",
    "vv.core.apps.CoreConfig",
    "vv.discussions.apps.DiscussionsConfig",
    "vv.manage_categories.apps.ManageCategoriesConfig",
    "vv.messaging.apps.MessagingConfig",
    "vv.users.apps.UsersConfig",
    "vv.vital_notif.apps.VitalNotifConfig",
]


ASGI_APPLICATION = "config.asgi.application"

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [(env("REDIS_HOST"), 6379)],
        },
    },
}

config/asgi.py

import os
import sys

import django
from django.core.asgi import get_asgi_application


app_path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir))
sys.path.append(os.path.join(app_path, "vv"))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.base")
django.setup()
django_asgi_app = get_asgi_application()


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

from config.routing import websocket_urlpatterns


application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": URLRouter(websocket_urlpatterns),
})

routing.py

from django.urls import path

from vv.messaging.consumers import MarkAsReadConsumer

websocket_urlpatterns = (
    path("ws/mark/", MarkAsReadConsumer.as_asgi()),
)

runner.sh

#!/bin/sh
daphne -b 0.0.0.0 -p 8000 config.asgi:application  -v2
python manage.py runworker -v2

client.js

let socket = new WebSocket("ws://<ip>/ws/mark/");

docker-compose.yml

version: "3.7"

x-env_file: &env_file
  env_file:
    - ./.env

services:
  nginx:
    build: ./nginx
    container_name: vv-fix-nginx
    ports:
     - "5555:80"
    depends_on:
     - backend
    networks:
     - vv-fix-network

  redis-vv:
    image: redis:alpine
    container_name: vv-fix-redis
    volumes:
      - vv_fix_redis_volume:/var/lib/redis/data/
    ports:
      - "6300:6379"
    networks:
      - vv-fix-network

  backend:
    <<: *env_file
    container_name: vv-fix-backend
    build:
      context: .
    depends_on:
      - db
      - redis-vv
    command: /bin/sh -c "/boot.sh"
    volumes:
      - .:/app
    networks:
      - vv-fix-network

volumes:
  vv_fix_db_volume:
  vv_fix_redis_volume:

networks:
  vv-fix-network

.env 文件中我有 REDIS_HOST=redis-vv

nginx.conf

events {}

http {
    client_max_body_size 25M;

    server {
        listen 80;
        listen [::]:80;

        access_log /var/log/nginx/reverse-access.log;
        error_log /var/log/nginx/reverse-error.log;
    
        location /ws/ {
              proxy_pass http://vv-fix-backend:8000;
          proxy_http_version  1.1;
          proxy_set_header    Upgrade $http_upgrade;
          proxy_set_header    Connection $connection_upgrade;
          proxy_set_header    Host $http_host;
          proxy_set_header    X-Real-IP $remote_addr;
         }  

        location / {
            proxy_pass http://vv-fix-backend:8000;
        }

    }
}

logs.txt

vv-fix-backend | 2021-09-14 16:17:13,228 DEBUG    Read environment variables from: .env
vv-fix-backend | 2021-09-14 16:17:13,229 DEBUG    get 'SECRET_KEY' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,229 DEBUG    get 'DEBUG' casted as '<class 'bool'>' with default 'False'
vv-fix-backend | 2021-09-14 16:17:13,229 DEBUG    get 'ALLOWED_HOSTS' casted as '<class 'list'>' with default '[]'
vv-fix-backend | 2021-09-14 16:17:13,229 DEBUG    get 'REDIS_HOST' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,229 DEBUG    get 'POSTGRES_DB' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,230 DEBUG    get 'POSTGRES_USER' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,230 DEBUG    get 'POSTGRES_PASSWORD' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,230 DEBUG    get 'POSTGRES_HOST' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,230 DEBUG    get 'POSTGRES_PORT' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,230 DEBUG    get 'CACHE_LOCATION' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,231 DEBUG    get 'BACKEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,231 DEBUG    get 'AWS_ENABLED' casted as 'None' with default 'False'
vv-fix-backend | 2021-09-14 16:17:13,231 DEBUG    get 'CELERY_BROKER_URL' casted as 'None' with default 'redis://redis:6379/1'
vv-fix-backend | 2021-09-14 16:17:13,231 DEBUG    get 'CELERY_TASK_DEFAULT_QUEUE' casted as 'None' with default 'default_queue'
vv-fix-backend | 2021-09-14 16:17:13,231 DEBUG    get 'AWS_REGION' casted as 'None' with default 'eu-central-1'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'EMAIL_HOST' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'EMAIL_USE_TLS' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'EMAIL_PORT' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'EMAIL_HOST_USER' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'EMAIL_HOST_PASSWORD' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'EMAIL_FROM_USER' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,232 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,233 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,233 DEBUG    get 'BACKEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,233 DEBUG    get 'DISCUSSION_ATTACHMENT_MAX_SIZE' casted as 'None' with default '5000000'
vv-fix-backend | 2021-09-14 16:17:13,233 DEBUG    get 'LOGGED_BEFORE_SECONDS' casted as 'None' with default '15'
vv-fix-backend | 2021-09-14 16:17:13,234 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,234 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,234 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,234 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,234 DEBUG    get 'FRONTEND_URL' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,234 DEBUG    get 'TWILIO_ACCOUNT_SID' casted as 'None' with default '<NoValue>'
vv-fix-backend | 2021-09-14 16:17:13,235 DEBUG    get 'TWILIO_AUTH_TOKEN' casted as 'None' with default '<NoValue>'

在Nginx中access.log我看到了

[17/Sep/2021:11:54:54 +0000] "GET /ws/mark/ HTTP/1.1" 404 1100 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36"

我用 uvicorn 试过了。不影响...

我在用

Python 3.8

Django 2.2

channels-redis 3.3.0

频道 3.0.4

redis 3.5.3

nginx.conf 位置更改为

        location / {
            proxy_pass http://vv-fix-backend:8010;
            proxy_http_version  1.1;
            proxy_set_header    Upgrade $http_upgrade;
            proxy_set_header    Connection "upgrade";
            proxy_set_header    Host $http_host;
            proxy_set_header    X-Real-IP $remote_addr;
        }

https://www.nginx.com/blog/websocket-nginx/