Django/Daphne/Nginx Websockets 未连接

发布于 2025-01-20 04:23:22 字数 2583 浏览 0 评论 0原文

我正在使用Django和Daphne和Nginx来运行我的Websocket。几个月来,一切都很好,但是几天前,我的Websockets不会连接说“ Websocket连接到'wss:// mydomain:8001/clips/clips/五十9/'失败:”。

我检查了服务器日志,这是在输出中,

2022-04-07 16:57:22,970 ERROR    [Failure instance: Traceback: <class 'AttributeError'>: 'NoneType' object has no attribute 'replace'
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/autobahn/websocket/protocol.py:2839:processHandshake
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/txaio/tx.py:366:as_future
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/ws_protocol.py:72:onConnect
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: --- <exception caught here> ---
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/server.py:200:create_application
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/routing.py:54:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:35:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:53:valid_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:72:validate_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:73:<genexpr>
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:97:match_allowed_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:376:urlparse
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:430:urlsplit
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: ]

我不明白为什么突然发生这种情况,这可能是依赖性被更新还是弃用了?

I am using Django with Daphne and Nginx to run my websockets. Everything was working fine for months in production, however just a couple days ago my websockets wouldn't connect saying "WebSocket connection to 'wss://mydomain:8001/clips/509/' failed: ".

I checked the server logs and this is in the output

2022-04-07 16:57:22,970 ERROR    [Failure instance: Traceback: <class 'AttributeError'>: 'NoneType' object has no attribute 'replace'
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/autobahn/websocket/protocol.py:2839:processHandshake
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/txaio/tx.py:366:as_future
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/ws_protocol.py:72:onConnect
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: --- <exception caught here> ---
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/server.py:200:create_application
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/routing.py:54:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:35:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:53:valid_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:72:validate_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:73:<genexpr>
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:97:match_allowed_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:376:urlparse
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:430:urlsplit
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: ]

I don't understand why this happened all of the sudden, is it maybe a dependency got updated or deprecated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2025-01-27 04:23:22

这是因为 urllib3 中的修复。允许的霍斯特罗素参与者是经纪的。我解决了此编辑的 asgi.py 文件。

我从此更改了“应用程序”定义:

import os

from .wsgi import *
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE',
    'manager.settings.production'
)

from .routing import websocket_urlpatterns
application = ProtocolTypeRouter({
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                websocket_urlpatterns
            )
        )
    ) 
})

到此处:

import os

from .wsgi import *
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE',
    'manager.settings.production'
)

from .routing import websocket_urlpatterns
application = ProtocolTypeRouter({
    "websocket": AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns
        ) 
    )
})

更多信息: https:// github .com/django/channels/essess/1716

This is because a fix in urllib3. AllowedHostsOriginValidator is broked. I solved this editing my asgi.py file.

I changed my "application" definition from this:

import os

from .wsgi import *
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE',
    'manager.settings.production'
)

from .routing import websocket_urlpatterns
application = ProtocolTypeRouter({
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                websocket_urlpatterns
            )
        )
    ) 
})

to this:

import os

from .wsgi import *
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault(
    'DJANGO_SETTINGS_MODULE',
    'manager.settings.production'
)

from .routing import websocket_urlpatterns
application = ProtocolTypeRouter({
    "websocket": AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns
        ) 
    )
})

More info here: https://github.com/django/channels/issues/1716

画▽骨i 2025-01-27 04:23:22

我通过编辑redis.service文件解决了这个问题

nano /etc/systemd/system/redis.service

execstop =/bin/kill -s term $ mainpid

execstartpost =/bin/sh -c“ echo $ mainpid&gt;/var/run/redis/redis.pid”

然后键入

sudo systemctl守护程序 - 雷亚德

sudo systemctl启用redis服务器

sudo systemctl restart redis.service

I solved this by editing the redis.service file

nano /etc/systemd/system/redis.service

ExecStop=/bin/kill -s TERM $MAINPID

ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"

then type

sudo systemctl daemon-reload

sudo systemctl enable redis-server

sudo systemctl restart redis.service

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文