简体   繁体   中英

django-channels nginx settings

My django app uses django-channels. I was able to configure django to run using gunicorn and nginx. The app run if i use python manage.py runserver and redis-server sends notification etc but i am unable to configure it using nginx.

 server {
    listen 80;
    server_name IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/amir/clientcode;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/amir/clientcode/adminpanel.sock;
    }
}

However when I try to configure it for django-channels it is giving me status 502

upstream channels-backend {
    server localhost:8000;
}

server {
    listen 80;
    server_name IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/amir/clientcode;
    }

    location / {
        try_files $uri @proxy_to_app;
        include proxy_params;
        proxy_pass http://unix:/home/amir/clientcode/adminpanel.sock;
    }

    location @proxy_to_app {
        proxy_pass http://channels-backend;

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

My asgi.py file

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "adminpanel.settings")
django.setup()
application = get_default_application()
``

First of all, install Daphne in your app: Here I use daphne==1.3.0

To start Daphne server, I use this command:

export DJANGO_SETTINGS_MODULE="config.settings"
exec daphne -b 0.0.0.0 --proxy-headers config.asgi:channel_layer

Besides Daphne, you have to start a worker:

python manage.py runworker

With this, you can use the sockets in the same URL's Projects.

Take a look in this article: https://medium.com/labcodes/introduction-to-django-channels-d1047e56f218

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM