【问题标题】:Nginx - Daphne deployment issueNginx - 达芙妮部署问题
【发布时间】:2020-12-01 21:27:08
【问题描述】:

我最近向 Django Web 应用程序添加了一个使用带有 Channels 的 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;
    }
}

达芙妮服务

[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 套接字的绑定,但都没有成功。

  1. 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 的消息。

  1. 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 daphne


    【解决方案1】:

    我也遇到了同样的问题,开始发疯了。

    您的 nginx 配置包含与我相同的错误语句:

    [...]
    proxy_set_header Connection “upgrade”;
    [...]
    

    升级这个词被错误的引号括起来。您需要在此处使用标准引号(ASCII 代码 34,键盘上的 Shift-2),而不是“花哨”的 unicode 引号。非常非常难找。

    一些网站似乎将标准引号转换为 unicode 转换引号,因为有人认为它们看起来更好......

    【讨论】:

    • 谢谢你,马库斯。您的解决方案就是答案!
    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    相关资源
    最近更新 更多