我正在努力在 Elastic Beanstalk 上部署我的 django 应用程序,使用 Gunicorn 作为我的 wsgi,使用 daphne 来处理 asgi。我设法部署了我的应用程序,但 Websocket 无法正常运行。
我能够通过在我的 Elastic beanstalk 环境中运行以下代码来测试 EC2 实例是否连接到 Redis 缓存:
eb ssh
source /var/app/venv/*/bin/activate
cd /var/app/current/
python manage.py shell
>>> import channels.layers
>>> from asgiref.sync import async_to_sync
>>> channel_layer = channels.layers.get_channel_layer()
>>> async_to_sync(channel_layer.send)('test_channel', {'foo': 'bar'})
>>> async_to_sync(channel_layer.receive)('test_channel')
>>> {'foo': 'bar'} # <---------- I was able to receive a response
但是,我无法在我的 EC2 实例上手动运行启动 daphne 服务器:
daphne -b 0.0.0.0 -p 5000 tattoovalley.asgi:application
上面的代码生成以下错误:ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
但是我已经在 .ebextensions 配置文件中配置了 DJANGO_SETTINGS_MODULE 环境模块: .ebextensions/01_env.config
option_settings:
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
value: static/
aws:elasticbeanstalk:container:python:
WSGIPath: tattoovalley/wsgi.py
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: tattoovalley.settings
PYTHONPATH: /opt/python/current/app/tattoovalley:$PYTHONPATH
aws:elbv2:listener:80:
DefaultProcess: http
ListenerEnabled: 'true'
Protocol: HTTP
Rules: ws
aws:elbv2:listenerrule:ws:
PathPatterns: /ws/*
Process: websocket
Priority: 1
aws:elasticbeanstalk:environment:process:http:
Port: '80'
Protocol: HTTP
aws:elasticbeanstalk:environment:process:websocket:
Port: '5000'
Protocol: HTTP
我还检查了端口 5000 是否处于活动状态,但没有发现任何活动。
可能是什么问题?有没有可以用来修复 websocket 连接的解决方案
这可能与您的 asgi.py 文件有关。 如果可以发的话我可以看一下。