Nginx - Daphne 部署问题
Nginx - Daphne deployment issue
我最近在 Django 网络应用程序中添加了一个使用带通道的 WebSocket 的功能,但遇到了一些问题。由于 Channels 和 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 套接字的绑定,但都没有成功。
- PORT 案例
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.
看来 Daphne 没有收到来自 Nginx 的消息。
- 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
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.
我需要一些故障排除建议。请随时询问任何可能有用的额外信息。
提前谢谢你。
我遇到了完全相同的问题并且开始发疯了。
您的 nginx 配置包含与我相同的错误语句:
[...]
proxy_set_header Connection “upgrade”;
[...]
upgrade 一词用错误的引号引起来。您需要在此处使用标准引号(ASCII 代码 34,键盘上的 Shift-2),而不是“花哨的”unicode 引号。非常非常难找。
有些网站似乎将标准引号转换为 unicode 移位引号,因为有人认为它们看起来更好...
我最近在 Django 网络应用程序中添加了一个使用带通道的 WebSocket 的功能,但遇到了一些问题。由于 Channels 和 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 套接字的绑定,但都没有成功。
- PORT 案例
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.
看来 Daphne 没有收到来自 Nginx 的消息。
- 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
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.
我需要一些故障排除建议。请随时询问任何可能有用的额外信息。
提前谢谢你。
我遇到了完全相同的问题并且开始发疯了。
您的 nginx 配置包含与我相同的错误语句:
[...]
proxy_set_header Connection “upgrade”;
[...]
upgrade 一词用错误的引号引起来。您需要在此处使用标准引号(ASCII 代码 34,键盘上的 Shift-2),而不是“花哨的”unicode 引号。非常非常难找。
有些网站似乎将标准引号转换为 unicode 移位引号,因为有人认为它们看起来更好...