websocket 使用nginx部署Django通道

lmvvr0a8  于 2022-11-11  发布在  Nginx
关注(0)|答案(1)|浏览(239)

我正在尝试使用Nginx在AWS ubuntu操作系统中部署Django应用程序。我已经在Nginx中配置了Django服务器。但我不知道如何在Nginx中配置通道或Redis服务器。

我的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-server连接,但当我用gunicorn app.wsgi -b 0.0.0.0:800运行服务器时,无法与webbsocket连接。
我也尝试了Hostinger VPS,但同样的问题。

f8rj6qna

f8rj6qna1#

您需要下载Daphne。
Daphne是Django频道的高性能WebSocket服务器。
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配置文件:

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

}

相关问题