【发布时间】:2020-12-16 20:27:45
【问题描述】:
我已通过 django 频道将聊天组件集成到我的 django web 应用程序中,其方式与文档中的 here 非常相似。我使用 elasticache 来创建我的 redis 实例。我的 settings.py 如下所示:
ASGI_APPLICATION = 'MyApp.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('my-redis-instance.cache.amazonaws.com', 6379)], #This is a placeholder in the question for the cluster's actual endpoint
},
},
}
在将我的应用程序部署到弹性 beantalk 时,我尝试关注 this tutorial 和 this tutorial,但没有成功。我的 django.config 文件目前如下所示:
container_commands:
01_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"
02_migrate:
command: "django-admin.py migrate"
leader_only: true
03_load-data:
command: "python manage.py load_database"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: MyApp.settings
aws:elasticbeanstalk:container:python:
WSGIPath: MyApp/wsgi.py
"aws:elasticbeanstalk:container:python:staticfiles":
/static/: "static/"
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
我还尝试创建一个 Procfile 来配置 gunicorn 和 daphne。它看起来像这样:
web: gunicorn --bind :8000 --workers 3 --threads 2 MyApp.wsgi:application
websocket: daphne -b 0.0.0.0 -p 5000 MyApp.asgi:application
附加到我的 ElastiCache redis 实例的安全组有一个入站规则,其中自定义 TCP 设置为端口 6379,源设置为任何。在尝试所有这些不同的部署方法时,我不断收到以下错误:
[Error] WebSocket connection to 'ws://my-site-url.com/ws/messages/' failed: Unexpected response code: 502
我不知道该怎么做。我看过几个与此相关的堆栈溢出帖子,但没有一个有帮助。这个post 似乎有一个有效的解决方案,但我相信它现在已经过时了。
更新:
在进行更多搜索时,我在我的 .ebextensions 文件夹中创建了一个 websockets-python.config 文件,该文件应该配置 Apache 代理服务器以允许使用 Web 套接字。我还删除了配置 Daphne 和 Gunicorn 的 Procfile。 websockets-python.config 文件如下所示:
files:
"/etc/httpd/conf.d/proxy-pass.conf":
mode: "000644"
owner: root
group: root
content: |
ProxyPass /ws/ ws://127.0.0.1:5000/
ProxyPassReverse /ws/ ws://127.0.0.1:5000/
"/etc/httpd/conf.modules.d/99-mod_proxy_wstunnel.conf":
mode: "000644"
owner: root
group: root
content: |
<IfModule !proxy_wstunnel_module>
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
</IfModule>
使用此文件重新部署后,我在 Web 套接字上收到无效状态错误,然后 Web 套接字连接最终超时。我相信网络套接字正在尝试连接,但由于某种原因无法连接(可能是我的安全组的配置方式),然后最终超时。
安全组配置:
ELB:从端口 80 上的任何地方的入站规则。从端口 80 和 5000 到任何地方的出站规则
我的服务器:ELB 在端口 80 和 5000 上的入站规则。所有流量到任何地方的出站规则。
ElastiCache 实例:来自任何地方的端口 6379 和来自服务器的端口 6439 的入站规则。 All Traffic to Anywhere 的出站规则。
【问题讨论】:
标签: django websocket amazon-elastic-beanstalk django-channels amazon-elasticache