(Nginx + Daphne + Django)WebSocket握手期间发生错误:意外的响应代码:404

我有一个使用Daphne作为应用程序容器和使用Nginx作为代理服务器的Django(3.0)应用程序。

达芙妮-u /home/ubuntu/virtualenvs/src/app/app.sock app.asgi:application

我的问题是websocket连接失败。

(索引):16 WebSocket与'ws://example.com/ws/chat/1/'的连接失败:WebSocket握手期间出错:意外的响应代码:404

我很确定我的应用程序设置和路由都很好。
因为如果我停止Nginx,将Daphne绑定到0.0.0.0:8000并使用真实IP(“ ws://xx.xx.xx.xx:8000 / ws / chat / 1”)作为URL,则将建立websocket连接,并且稳定。
我应该如何修改Nginx以使Websocket正常工作?

#my nginx setting

upstream websocket {
    server unix:/home/ubuntu/virtualenvs/src/app/app.sock;
}
#websocket settings
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
listen 80;
listen [::]:80 default_server;
listen 443 SSL default_server;
listen [::]:443 SSL default_server;
server_name example.com;
return 301 https://example.com$request_uri;
ssl on;
# certificate settings
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_session_timeout 10m;
#ssl_session cache shared:SSL:1m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
#ssl_prefer_server_ciphers on;
#ssl_ciphers ecdhe-RSA-AES128-GCM-SHA256:ecdhe:ECDH:AES:HIGH:!NULL:!aNULL:!$

location / {
    root /home/ubuntu/virtualenvs/
    include proxy_params;
    proxy_pass https://websocket;

    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-Proto $scheme;
    proxy_set_header X-Frame-Options SAMEORIGIN;
    proxy_pass_request_headers on;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }

location ~* \.(js|css)$ {
    expires -1;
  }
}
yqfjh 回答:(Nginx + Daphne + Django)WebSocket握手期间发生错误:意外的响应代码:404

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2779225.html

大家都在问