Развертывание каналов Django с помощью nginx

Я пытаюсь развернуть приложение Django в AWS ubuntu os с каналами Django, используя Nginx. Я настроил Django сервер в Nginx. Но я не знаю, как настроить каналы или Redis-сервер в Nginx.

Мой конфиг nginx следующий:

server {
    listen 80;
    server_name 52.77.215.218;

    location / {
        include proxy_params;
        proxy_pass http://localhost:8000/
    }
}

Мой settings.py:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    },
}

Мои требования.txt:

aioredis==1.3.1
asgiref==3.5.2
async-timeout==4.0.2
attrs==22.1.0
autobahn==22.7.1
Automat==20.2.0
certifi==2022.6.15
cffi==1.15.1
channels==3.0.5
channels-redis==2.4.2
charset-normalizer==2.1.1
constantly==15.1.0
coreapi==2.3.3
coreschema==0.0.4
cryptography==37.0.4
daphne==3.0.2
defusedxml==0.7.1
Django==4.1
django-cors-headers==3.13.0
django-templated-mail==1.1.1
djangorestframework==3.13.1
djangorestframework-simplejwt==4.8.0
djoser==2.1.0
gunicorn==20.1.0
hiredis==2.0.0
hyperlink==21.0.0
idna==3.3
incremental==21.3.0
itypes==1.2.0
Jinja2==3.1.2
MarkupSafe==2.1.1
msgpack==0.6.2
mysql==0.0.3
mysqlclient==2.1.1
oauthlib==3.2.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
PyJWT==2.4.0
pyOpenSSL==22.0.0
python3-openid==3.2.0
pytz==2022.2.1
requests==2.28.1
requests-oauthlib==1.3.1
service-identity==21.1.0
six==1.16.0
social-auth-app-django==4.0.0
social-auth-core==4.3.0
sqlparse==0.4.2
Twisted==22.4.0
twisted-iocpsupport==1.0.2
txaio==22.2.1
typing_extensions==4.3.0
tzdata==2022.2
uritemplate==4.1.1
urllib3==1.26.12
zope.interface==5.4.0

Когда я запускаю сервер с python3 manage.py runserver 0.0.0.0:8000. Сервер работает хорошо и также соединяется с redis-сервером, но когда я запускаю сервер с gunicorn app.wsgi -b 0.0.0.0:800, то не удается соединиться с webbsocket.
Я также пробовал Hostinger VPS, но та же проблема.

Вам необходимо загрузить Daphne.

Daphne - это высокопроизводительный веб-сокет сервер для каналов Django.

https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/daphne/

https://github.com/django/daphne

https://channels.readthedocs.io/en/stable/deploying.html

Как можно управлять Дафной: daphne -b 0.0.0.0 -p 8070 django_project.asgi:application

Вот мой Nginx conf для каналов:

upstream django {
    server 127.0.0.1:8080;
}
upstream websockets{
    server 127.0.0.1:8070;
}

server {
    ...
    ...
   

    location /ws {
        proxy_pass http://websockets;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        ...
    }

    location / {
        proxy_pass http://django; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        ...
    }

}

Вернуться на верх