繁体   English   中英

如何配置uwsgi + nginx + Django + websocket?

[英]How to Configure uwsgi + nginx + Django + websocket?

我成功部署了uwsgi + nginx + Django。 但我想使用 websocket 与我的服务器通信。

我按照本教程处理正常的 https 请求。 https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

nginx conf文件设置如下:

upstream django {
    server unix:///path/to/your/mysite/mysite.sock; # for a file socket
}

# configuration of the server
server {
    # the port your site will be served on
    listen 443 ssl; # managed by Certbot
    # the domain name it will serve for
    server_name example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;



    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

我用谷歌搜索了很多,发现人们建议添加这些设置:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;

这没用。 我没有使用proxy_pass因为我已经在使用uwsgi_pass django; 我的客户尝试使用 websocket 连接到同一个 url(位置)我 uwsgi_pass 到 django。 我查看了 uwsgi 文档并找到了这个页面: https://uwsgi-docs.readthedocs.io/en/latest/WebSockets.html

根据这个页面,如果我只添加 --http-websockets 选项,我不需要额外的设置。 所以我做了。 像这样:

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --http-websockets www-data --daemonize /var/log/uwsgi-emperor.log

它被添加到 /etc/rc.local 中。 该命令也不起作用。 我还尝试将http-websockets = true添加到.ini 文件。 它甚至使 uwsgi 停止。 所以我删除了那个选项。

这里有什么问题?

对于每个 websocket 请求,我都会收到 400 个错误请求。 我看到的唯一数据是(操作码 -1)。 没有 graphql 错误消息。

您需要将套接字类型从.sock 更改为 net (ip:port)。 并在 NGinx “代理”和带有 django 的服务器上使用此结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM