Unable to connect to websocket using django channels, nginx on docker as services

I am using docker compose to create a project using django, nginx as services. When I start the Daphne server and the client tries to connect to the websocket server, I get this error:

*1 recv() failed (104: Connection reset by peer) while reading response header from upstream

The client side shows this.

failed: Error during WebSocket handshake: Unexpected response code: 502

Here is my 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

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;
    }

And the circusd web.ini file:

[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
+6
source share
1 answer

As explicitly stated in the excellent guide , to successfully launch the channels you need to have a dedicated application server that implements the ASGI protocol, such as the supplied onedaphne

Django , " ", , , WebSockets HTTP SMS, " ", (, ...). "", .

3 , ASGI :

  • , ( )
  • IPC,
  • , redis, , .

, DATABASES::

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

, , docker nginx ( - ) , , redis connectin all them.

, , , , nginx ASGI, django- http- .

.

+1

Source: https://habr.com/ru/post/1016376/


All Articles