最近,我使用WebSocket向Django web应用程序添加了一个带有通道的函数,并遇到了一些问题。因为通道和WebSocket在本地测试服务器(manage.py runserver)中工作得很好,所以可以看出部署部分是负责的。
下面是一些设置文件和状态检查:
nginx_conf.conf
#added this block
upstream channels-backend {
server localhost:9001;
}
server {
listen 80;
server_name MY_URL;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/MY_USER/server/mysite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/MY_USER/server/mysite/mysite.sock;
}
#path to proxy my WebSocket requests
location /ws/ {
# proxy_pass http://unix:/home/MY_USER/server/mysite/mysite_w.sock;
proxy_pass http://channels-backend;
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;
}
}daphne.service
[Unit]
Description=daphne daemon
After=network.target
[Service]
User=MY_USER
Group=www-data
WorkingDirectory=/home/MY_USER/server/mysite
#ExecStart=/home/MY_USER/server/server/bin/daphne -u /home/MY_USER/server/mysite/mysite_w.sock mysite.asgi:application -v2
ExecStart=/home/MY_USER/server/server/bin/daphne -p 9001 mysite.asgi:application
[Install]
WantedBy=multi-user.target如您所见,我已经测试了端口和UNIX套接字的绑定,但都没有成功。
Chrome控制台消息
(index):16 WebSocket connection to 'ws://MY_URL/ws/chat/test/' failed: Error during WebSocket handshake: Unexpected response code: 400
(anonymous) @ (index):16
(index):30 Chat socket closed unexpectedly
chatSocket.onclose @ (index):30
2(index):43 WebSocket is already in CLOSING or CLOSED state.❯ sudo less /var/log/nginx/access.log
61.82.112.1 - - [12/Aug/2020:06:27:29 +0000] "GET /chat/test/ HTTP/1.1" 200 682 "http://MY_URL/chat/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"
61.82.112.1 - - [12/Aug/2020:06:27:30 +0000] "GET /ws/chat/test/ HTTP/1.1" 400 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"❯ sudo journalctl -u mysite-daphne.service
Aug 10 09:31:32 hermes systemd[1]: mysite-daphne.service: Succeeded.
Aug 10 09:31:32 hermes systemd[1]: Stopped daphne daemon.
Aug 10 09:31:32 hermes systemd[1]: Started daphne daemon.看起来达芙妮没有收到Nginx的信息。
Chrome控制台消息
(index):16 WebSocket connection to 'ws://MY_URL/ws/chat/test/' failed: Error during WebSocket handshake: Unexpected response code: 400
(anonymous) @ (index):16
(index):30 Chat socket closed unexpectedly
chatSocket.onclose @ (index):30
3(index):43 WebSocket is already in CLOSING or CLOSED state.
document.querySelector.onclick @ (index):43
document.querySelector.onkeyup @ (index):36❯ sudo less /var/log/nginx/access.log
61.82.112.1 - - [12/Aug/2020:06:42:44 +0000] "GET /ws/chat/test/ HTTP/1.1" 400 5 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Mobile Safari/537.36"❯ sudo journalctl -u mysite-daphne.service
Aug 10 09:31:32 hermes systemd[1]: Stopped daphne daemon.
Aug 10 09:31:32 hermes systemd[1]: Started daphne daemon.我需要一些关于故障排除的建议。请随时询问任何可能有帮助的额外信息。
提前谢谢你。
发布于 2021-02-15 09:01:17
我也有同样的问题,开始发疯了。
您的nginx配置包含与我的配置相同的错误语句:
[...]
proxy_set_header Connection “upgrade”;
[...]升级一词用错误的引号括起来。这里需要有标准引号(ASCII代码34,Shift-2在键盘上),而不是“花哨”unicode引号。很难找到。
一些网站似乎将标准引号转换为unicode移位引号,因为有人认为它们看起来更好。
https://stackoverflow.com/questions/63371298
复制相似问题