AWS EC2 Ubuntu 实例(Django 通道)上的 Websocket 连接失败
Websocket Connection Failed on AWS EC2 Ubuntu Instance(Django Channels)
我正在尝试在 ubuntu aws ec2 实例中部署我的 django chammels asgi 应用程序,我的 wsgi 应用程序工作正常而且 ubuntu 上的 asgi 工作正常(使用 [=34= 测试] websocket 库)代码没有错误,daphne 是 运行 完美
我也启用了EC2安全组中的所有端口
代码库没有错误,而且 daphne 在 EC2 的本地主机上 运行 ubuntu 完美实例
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 脚本来确定我的达芙妮是否正常工作,它为我提供了完美的结果,没有错误
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())
以下脚本returns
{
"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(){
...
}
...
但是下面的脚本returns
WebSocket connection to 'ws://<SERVER_IP>/ws/chat/person/?token=<TOKEN> failed:
我使用主管解决了这个问题
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
我正在尝试在 ubuntu aws ec2 实例中部署我的 django chammels asgi 应用程序,我的 wsgi 应用程序工作正常而且 ubuntu 上的 asgi 工作正常(使用 [=34= 测试] websocket 库)代码没有错误,daphne 是 运行 完美
我也启用了EC2安全组中的所有端口
代码库没有错误,而且 daphne 在 EC2 的本地主机上 运行 ubuntu 完美实例
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 脚本来确定我的达芙妮是否正常工作,它为我提供了完美的结果,没有错误
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())
以下脚本returns
{
"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(){
...
}
...
但是下面的脚本returns
WebSocket connection to 'ws://<SERVER_IP>/ws/chat/person/?token=<TOKEN> failed:
我使用主管解决了这个问题
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