Can not connect to websocket using django-channels , nginx on docker as services -
i'm using docker compose build project django, nginx services. when launch daphne server, , client tries connect websocket server, error:
*1 recv() failed (104: connection reset peer) while reading response header upstream
client-side shows this
failed: error during websocket handshake: unexpected response code: 502
here 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 circusd's 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
as quite explicitly stated in fine manual, run channels need have dedicated application server implementing asgi protocol, such supplied daphne
the entire django execution model has been changed channels, there separate "interface servers" taking care of receiving , sending messages over, example, websockets or http or sms, , "worker servers" run actual code (potentially on different server or vm or container or...). 2 connected "channel layer" carries messages , replies , forth.
the current implementation supplies 3 channel layers talk asgi between interface server , worker server:
- an in-memory channel layer, used running test server (it's single process)
- an ipc based channel layer, usable run different workers on same server
- a redis based channel layer, should used heavy production sites, able connect interface servers multiple worker servers.
you configure them databases::
channel_layers = { "default": { "backend": "asgi_redis.redischannellayer", "routing": "my_project.routing.channel_routing", "config": { "hosts": [("redis-channel-1", 6379), ("redis-channel-2", 6379)], }, }, }
of course means docker config has change , add 1 or more interface servers instead of, or in addition to, nginx
(even if, in case, you'll need accept websocket connections on different port connected possible problems) and, quite likely, instance of redis connectin them.
this in turn means until circus , nginx can support asgi, won't possible use them django-channels, or support regular http part of system.
you can find more info in deploying section of official documentation.
Comments
Post a Comment