【问题标题】:Websocket Connection Failed on AWS EC2 Ubuntu Instance(Django Channels)AWS EC2 Ubuntu 实例(Django 通道)上的 Websocket 连接失败
【发布时间】:2023-03-09 11:29:02
【问题描述】:

我正在尝试在 ubuntu aws ec2 实例中部署我的 django chammels asgi 应用程序,我的 wsgi 应用程序运行良好,ubuntu 上的 asgi 运行良好(使用 python websocket 库测试)代码中没有错误并且 daphne 正在运行完美

我也启用了 EC2 安全组中的所有端口

代码库没有错误,daphne 也完美运行在 EC2 ubuntu 实例的 localhost 上

server {
    listen 80;
    server_name MY_SERVER_IP;

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    location /ws/{
        proxy_pass http://0.0.0.0:4001;
        proxy_buffering off;
        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;
    }
}

[Unit]
Description=project Daphne Service
After=network.target

[Service]
User=root
Type=simple
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/venv/bin/python /home/ubuntu/myproject/venv/bin/d>

[Install]
WantedBy=multi-user.target

我创建了这个 python 脚本来确定我的 daphne 是否工作,它为我提供了完美的结果,没有错误

import asyncio
import websockets
import json
async def hello():
    uri = "ws://127.0.0.1:4001/ws/chat/person/?token=<CHAT_TOKEN>
    async with websockets.connect(uri) as websocket:
          await websocket.send(json.dumps({"message":"Hey there"}))
          g = await websocket.recv()
          print(g)
asyncio.get_event_loop().run_until_complete(hello())

以下脚本返回

{
  "id": 1,
  "message": "Hey There",
  "time_stamp": "TIME",
  "receiver": {
     "username": "John Doe",
     "id": 2",
     "profile_picture": "LINK"
  }
}

当我尝试连接 websocket 时,现在在我的前端应用程序中

socket = new WebSocket("ws://<SERVER_IP>:4001/ws/chat/person/?token=<MY_TOKEN>")

socket.onopen = functon(){
...
}
...

但是下面的脚本返回

WebSocket connection to 'ws://<SERVER_IP>/ws/chat/person/?token=<TOKEN> failed:

【问题讨论】:

    标签: django amazon-web-services amazon-ec2 websocket daphne


    【解决方案1】:

    我通过使用主管解决了这个问题

    sudo apt install nginx supervisor
    

    现在,您需要创建主管配置文件(通常位于 /etc/supervisor/conf.d/ - 在这里,我们让主管在 TCP 端口上侦听,然后将该套接字交给子进程,以便它们都可以共享相同的绑定端口:

    [fcgi-program:asgi]
    # TCP socket used by Nginx backend upstream
    socket=tcp://localhost:8000
    
    # Directory where your site's project files are located
    directory=/my/app/path
    
    # Each process needs to have a separate socket file, so we use process_num
    # Make sure to update "mysite.asgi" to match your project name
    command=daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers mysite.asgi:application
    
    # Number of processes to startup, roughly the number of CPUs you have
    numprocs=4
    
    # Give each process a unique name so they can be told apart
    process_name=asgi%(process_num)d
    
    # Automatically start and recover processes
    autostart=true
    autorestart=true
    
    # Choose where you want your log to go
    stdout_logfile=/your/log/asgi.log
    redirect_stderr=true
    

    为主管配置文件中引用的套接字创建运行目录。

    sudo mkdir /run/daphne/
    sudo chown <user>.<group> /run/daphne/
    d /run/daphne 0755 <user> <group>
    sudo supervisorctl reread
    sudo supervisorctl update
    

    【讨论】:

      猜你喜欢
      • 2023-02-25
      • 2020-05-18
      • 1970-01-01
      • 2023-01-24
      • 2022-10-13
      • 2019-01-27
      • 2015-04-17
      • 2014-04-12
      • 2018-01-17
      相关资源
      最近更新 更多