无法使用django-channels,docker上的nginx作为服务连接到websocket

Асх*_*лов 6 django nginx docker circusd devops

我正在使用docker compose来构建一个django,nginx作为服务的项目.当我启动daphne服务器,并且客户端尝试连接到websocket服务器时,我收到此错误:

*1 recv() failed (104: Connection reset by peer) while reading response header from upstream
Run Code Online (Sandbox Code Playgroud)

客户端显示了这一点

failed: Error during WebSocket handshake: Unexpected response code: 502
Run Code Online (Sandbox Code Playgroud)

这是我的docker-compose.yml

version: '3'
services:
  nginx:
    image: nginx
    command: nginx -g 'daemon off;'
    ports:
      - "1010:80"
    volumes:
      - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf
      - .:/makeup
    links:
      - web
  web:
    build: .
    command: /usr/local/bin/circusd /makeup/config/circus/web.ini
    environment:
      DJANGO_SETTINGS_MODULE: MakeUp.settings
      DEBUG_MODE: 1
    volumes:
      - .:/makeup
    expose:
      - '8000'
      - '8001'
    links:
      - cache
    extra_hosts:
      "postgre": 100.73.138.65
Run Code Online (Sandbox Code Playgroud)

Nginx的:

server {
        listen 80;
        server_name thelab518.cloudapp.net;

        keepalive_timeout 15;
        root /makeup/;

        access_log /dev/stdout;
        error_log /dev/stderr;

        location /api/stream {
            proxy_pass http://web:8001;
            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 / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://web:8000;
    }
Run Code Online (Sandbox Code Playgroud)

而circusd的web.ini文件:

[watcher:web]
cmd = /usr/local/bin/gunicorn MakeUp.wsgi:application -c config/gunicorn.py
working_dir = /makeup/
copy_env = True
user = www-data

[watcher:daphne]
cmd = /usr/local/bin/daphne -b 0.0.0.0 -p 8001 MakeUp.asgi:channel_layer
working_dir = /makeup/
copy_env = True
user = root

[watcher:worker]
cmd = /usr/bin/python3 manage.py runworker
working_dir = /makeup/
copy_env = True
user = www-data
Run Code Online (Sandbox Code Playgroud)

Rob*_*obM 1

正如精美手册中明确指出的那样,要成功运行 Channels,您需要有一个实现 ASGI 协议的专用应用程序服务器,例如提供的daphne

整个 Django 执行模型已通过 Channels 进行了更改,因此有单独的“接口服务器”负责通过 WebSockets 或 HTTP 或 SMS 等方式接收和发送消息,以及运行实际代码的“工作服务器”(可能是在不同的服务器或虚拟机或容器或...)。两者通过“通道层”连接,来回传送消息和回复。

当前的实现提供了 3 个通道层,用于在接口服务器和工作服务器之间进行 ASGI 通信:

  • 内存通道层,主要用于运行测试服务器(它是单个进程)
  • 基于 IPC 的通道层,可用于在同一服务器上运行不同的工作进程
  • 基于 Redis 的通道层,应用于重型生产站点,能够将接口服务器连接到多个工作服务器。

您可以像配置数据库一样配置它们::

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "ROUTING": "my_project.routing.channel_routing",
        "CONFIG": {
            "hosts": [("redis-channel-1", 6379), ("redis-channel-2", 6379)],
        },
    },
}
Run Code Online (Sandbox Code Playgroud)

当然,这意味着你的 docker 配置必须更改并添加一个或多个接口服务器,而不是nginx(即使在这种情况下,你需要在不同的端口上接受 websocket 连接)可能的问题),并且很可能是一个连接所有这些问题的 redis 实例。

这反过来意味着,在 circus 和 nginx 可以支持 ASGI 之前,不可能将它们与 django-channels 一起使用,或者这种支持仅适用于系统的常规 http 部分。

您可以在官方文档的部署部分找到更多信息。