【问题标题】:WebSocket connection does not happen and connection request gets 400 error NGINX+Daphne+Django+Channels on Ubuntu AWS EC2WebSocket 连接没有发生,连接请求在 Ubuntu AWS EC2 上收到 400 错误 NGINX+Daphne+Django+Channels
【发布时间】:2022-02-20 17:53:53
【问题描述】:

我按照教程使用 Daphne 和 Nginx 为 Django 通道部署 websockets。 这是我在 etc/nginx/sites-enabled/ 上的 NGINX.conf 的样子


server {
    listen 80;
    server_name my_server_ip;
    charset utf-8;

    client_max_body_size 128M;

    location /static {
    # exact path to where your static files are located on server
    # [mostly you won't need this, as you will be using some storage service for same]
        location /static {
                 alias /mywebsite/static;
        }

    }

    location /media {
    # exact path to where your media files are located on server
    # [mostly you won't need this, as you will be using some storage service for same]
        alias /mywebsite/media;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/mywebsite/run/uwsgi.sock;
        uwsgi_read_timeout 300s;
    
    #path to proxy my WebSocket requests
    location /ws/ {
        proxy_pass http://0.0.0.0:9001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection “upgrade”;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }

    access_log /mywebsite/log/dev-nginx-access.log;
    error_log /mywebsite/log/dev-nginx-error.log;
}

现在我的 daphne.service 文件如下所示

[Unit]
Description=Mywebsite Daphne Service for websockets
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/mywebsite/
#edit myproject
#If your daphne is not installed globally and is inside your #environment, enter the complete path to daphne
ExecStart=/mywebsite/bin/daphne -b 0.0.0.0 -p 9001 mywebsite.asgi:application
Restart=on-failure
#KillSignal=SIGQUIT
#Type=notify
#NotifyAccess=all

[Install]
WantedBy=multi-user.target

asgi.py 文件如下所示

"""
ASGI config for mywebsite project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import chatapp.routing

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mywebsite.settings")

application = ProtocolTypeRouter({
  "http": get_asgi_application(),
  "websocket": AuthMiddlewareStack(
        URLRouter(
            dark.routing.websocket_urlpatterns
        )
    ),
})

我的 routing.py 文件如下所示

# chatapp/routing.py
from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
    re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()),
]

我上面配置的 JavaScript 代码是ws://my_server_ip:9001/ws/chat/lobby/

整个设置在没有 NGINX 的情况下也可以工作我的意思是,如果我只做 python manage.py runserver 0.0.0.0:8080 并在我的 JavaScript 中将 WebSocket 代码设置为 ws://my_server_ip:8080/ws/chat/lobby/ 它工作正常且流畅

除此之外,我的 webapp 的 wsgi 方面正在 uwsgi 上运行并且运行得非常成功

我已将它部署在 AWS EC2 实例上的 Ubuntu 20.04 上。我在通过 NGINX 连接我的 WebSocket 时遇到了麻烦。

当我检查错误日志时,它什么也没有显示,而在 access.log 中它显示为

42.105.172.132 - - [19/Feb/2022:19:26:48 +0000] "GET /dark/inbox/notifications/api/unread_list/?max=10 HTTP/1.1" 200 38 "http://my_ip_address.com/dark/app-chat/" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"
42.105.172.132 - - [19/Feb/2022:19:26:53 +0000] "GET /dark/inbox/notifications/api/unread_list/?max=10 HTTP/1.1" 200 38 "http://my_ip_address.com/dark/app-chat/" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"
---------------focus on line below this is the access log giving 400 status---------------------
***42.105.172.132 - - [19/Feb/2022:19:27:00 +0000] "GET /ws/chat/lobby/ HTTP/1.1" 400 5 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"***
------------------------------------------------------------------------------------------------
42.105.172.132 - - [19/Feb/2022:19:27:01 +0000] "GET /dark/inbox/notifications/api/unread_list/?max=10 HTTP/1.1" 200 38 "http://my_ip_address.com/dark/app-chat/" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Mobile Safari/537.36"


我花了几天时间寻找解决方案,但找不到解决方案。我该如何解决这个问题?我还检查了daphne、nginx、uwsgi、redis的所有服务文件的状态都很好,并且可以通过systemctl启动。

我的 daphne.service 和/或 nginx.conf 中是否有任何错误? 无论我能得到什么方向,我都会感激不尽。

【问题讨论】:

    标签: amazon-web-services nginx websocket django-channels daphne


    【解决方案1】:

    我发布这个答案,以便它对面临我所面临问题的每个人都有用......我需要做的唯一一点就是在 ec2 管理控制台上打开 9001 端口,就是这样!这解决了这个问题! 因此,您应该在 -p 之后打开您在 daphne -b 0.0.0.0 -p 9001 mywebsite.asgi:application 中的任何帖子,这里是 9001

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2020-10-26
      • 2022-01-12
      • 2019-05-03
      • 2022-06-29
      • 2023-03-09
      • 2021-07-03
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多