Websocket 在部署后给出错误 500,但在开发上工作

Websocket gives error 500 once deployed but works on dev

我正在尝试使用 django-channels 部署一个 django 应用程序。当我 运行 服务器在开发环境中时,它工作得很好。但是在部署时,websocket 崩溃并出现错误 500。

我在 linux debian stretch 9.9 VPS 和 python 3.6 上使用 django 2.2.2、通道 2.2.0 和 asgiref 3.1.4(我自己编译,因为不是可用)和 nginx 引擎。

我关注 https://djangodeployment.readthedocs.io/en/latest/01-getting-started.html and for the channels i tried to adapt the page https://channels.readthedocs.io/en/latest/deploying.html but mostly https://avilpage.com/2018/05/deploying-scaling-django-channels.html

上的部署教程

我对 redis 和所有这些东西很菜鸟,起初我让 redis 在端口 6379 上工作,但它是与系统的主要用户一起使用的,我曾经在开发环境中进行过一些测试,而不是django 应用程序的用户。因此,我为 django 应用程序用户在端口 6479 上对 运行 docker 执行了另一个命令,这似乎有效。然后我有 404 错误,但这是因为 nginx 没有为 websocket 正确配置。现在,我遇到了 500 错误。

这是我尝试打开 websocket 时 django 的回溯:

[2019-08-19 12:54:46,857] ERROR: Exception inside application: Connection has been closed by server
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels/sessions.py", line 183, in __call__
    return await self.inner(receive, self.send)
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels/middleware.py", line 41, in coroutine_call
    await inner_instance(receive, send)
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels/consumer.py", line 59, in __call__
    [receive, self.channel_receive], self.dispatch
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels/utils.py", line 59, in await_many_dispatch
    await task
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels_redis/core.py", line 429, in receive
    real_channel
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels_redis/core.py", line 484, in receive_single
    index, channel_key, timeout=self.brpop_timeout
  File "/opt/knightools/venv/lib/python3.6/site-packages/channels_redis/core.py", line 327, in _brpop_with_clean
    await connection.eval(cleanup_script, keys=[], args=[channel, backup_queue])
  Connection has been closed by server

根据我在网上看到的,这可能是redis configuration/installation的问题,但是为什么当运行与manage.py runserver结合时它可以正常工作? 运行 和 docker 的正确方法是什么?我看到跟踪从会话开始,所以也许我对它们有疑问,但它们正确存储在数据库中(据我所知)。

我完全迷路了,如果你能帮助我,谢谢你。

好的,经过更多测试后,我发现甚至 manage.py runserver 也不再工作了。现在它可以工作了,所以我就是这样做的,以防其他人遇到这个麻烦:

我使用以下命令清除了每个 docker:

docker ps

最后一栏是名字。然后,对于每个名字,执行:

docker stop <name>
docker rm <name>

完成后,我刚刚使用 django-app 用户启动了带有 6379 端口的默认 redis 服务器:

docker run -p 6379:6379 -d redis:2.8

相应地更新设置,如果必须重新编译设置,重新加载有关 django-app 的服务,那么它应该可以工作(至少对我有用)。

正如我所说,对redis或docker一无所知,所以可能有冲突。

希望对您有所帮助。