Nginx-达芙妮部署问题

问题描述

我最近在Django Web应用程序中添加一个将WebSocket与Channels结合使用的功能,但遇到了一些麻烦。由于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套接字的绑定,但都没有成功。

  1. 端口保护套
    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收到消息。

  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 配置包含和我一样的错误语句:

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

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

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