question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Channels 2 + Daphne + Nginx (using Docker)

See original GitHub issue

I am trying to configure Channels 2 using Nginx as reverse proxy for Daphne. This is my compose basic configuration:

base.yml

  django: &django
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    depends_on:
      - postgres
      - redis
    command: /gunicorn.sh

  nginx:
    build: ./compose/nginx
    depends_on:
      - django
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"

  redis:
    image: redis:latest

  channelworker:
    <<: *django
    command: /start-channelworker.sh
    depends_on:
      - postgres
      - redis

  channelserver:
    <<: *django
    command: /start-channelserver.sh
    depends_on:
      - postgres
      - redis

start-channelserver.sh daphne -u /tmp/daphne.sock config.asgi:application -v 2

start-channelworker.sh python manage.py runworker channels

nginx.conf

http {
    upstream app {
        server django:5000;
    }

    upstream ws_server {
        server unix:/tmp/daphne.sock;
    }

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

        # listen on both hosts
        server_name 192.168.99.100;

        # redirect to the https host (declared below)
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl;
        listen [::]:443 ssl;

        # The host name to respond to
        server_name 192.168.99.100;
   
       ... SSL configuration omitted ...

        location / {
            try_files $uri @proxy_to_app;
        }

        location /ws/ {
            try_files $uri @proxy_to_ws;
        }

        location @proxy_to_ws {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_redirect off;

            proxy_pass   http://ws_server;
        }

        # django app
        location @proxy_to_app {
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Url-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            proxy_pass   http://app;
        }
    }
}

Logs

channelserver

channelserver_1  | 2018-02-20 14:09:08,842 INFO     Configuring endpoint unix:/tmp/daphne.sock
channelserver_1  | 2018-02-20 14:09:08,843 INFO     HTTPFactory starting on '/tmp/daphne.sock'
channelserver_1  | 2018-02-20 14:09:08,844 INFO     Starting factory <daphne.http_protocol.HTTPFactory object at 0x7f56691a17f0>

channelworker 2018-02-20 14:06:12,382 - INFO - runworker - Running worker for channels ['channels']

nginx 200 error responses (websocket failed connection)

What am I missing? I believe there’s some communication problem between nginx and channelserver but I do not understand how to debug this.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
pplonskicommented, Oct 25, 2018

I set an example how to set docker-compose with Django Channels 2 on daphne and nginx. For HTTP I’m doing nginx proxy to gunicorn (however can be changed to daphne as well).

Below my nginx config:


upstream app {
    server wsgiserver:8000;
}

upstream ws_server {
    server asgiserver:9000;
}


server {
    listen 8000 default_server;
    listen [::]:8000;

    client_max_body_size 20M;

    location / {
        try_files $uri @proxy_to_app;
    }

    location /tasks {
        try_files $uri @proxy_to_ws;
    }

    location @proxy_to_ws {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;

        proxy_pass   http://ws_server;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass   http://app;
    }

}

0reactions
harem234commented, Mar 20, 2020

for simple reproduce see this: https://github.com/django/channels/issues/1433

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deploy to docker with nginx, django, daphne - YouTube
Django : Deploy to docker with nginx, django, daphne [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] Django : Deploy ...
Read more >
Deploy to docker with nginx, django, daphne - Stack Overflow
The Nginx website has some helpful tips for deploying with Docker that you should read, including a sample, very simple Dockerfile:.
Read more >
[Answered]-Deploy to docker with nginx, django, daphne-django
I want to deploy my service to docker. and my service is developed using python+django and django-channels.
Read more >
How will the configuration for channels be for supervisor ...
hello, i was trying to deploy django-channels but couldn't. Does anyone have sample for running daphne in supervisor and nginx configuration.
Read more >
Production Django, WebSockets, Django Channels 2, Daphne ...
Production configuration for a django website that uses websockets with django channels 2, Daphne, Bash script to run daphne service, Nginx, Gunicorn, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found