简体   繁体   English

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

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

I successfully deployed uwsgi + nginx + Django.我成功部署了uwsgi + nginx + Django。 But I want to communicate with my server using websocket.但我想使用 websocket 与我的服务器通信。

I followed this tutorial to handle normal https requests.我按照本教程处理正常的 https 请求。 https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

The nginx conf file settings are like this: 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
    }
}

I Googled a lot and found that people recommended to add these settings:我用谷歌搜索了很多,发现人们建议添加这些设置:

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

It does not work.这没用。 I did not use proxy_pass because I am already using uwsgi_pass django;我没有使用proxy_pass因为我已经在使用uwsgi_pass django; and my client attempts to connect using websocket to the same url (location) I uwsgi_pass to django.我的客户尝试使用 websocket 连接到同一个 url(位置)我 uwsgi_pass 到 django。 I looked into uwsgi docs and found this page: https://uwsgi-docs.readthedocs.io/en/latest/WebSockets.html我查看了 uwsgi 文档并找到了这个页面: https://uwsgi-docs.readthedocs.io/en/latest/WebSockets.html

According to this page, I do not need additional settings if I just add --http-websockets option.根据这个页面,如果我只添加 --http-websockets 选项,我不需要额外的设置。 So I did.所以我做了。 Like this:像这样:

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

It is added in /etc/rc.local.它被添加到 /etc/rc.local 中。 This command does not work either.该命令也不起作用。 I also tried to add http-websockets = true to.ini file.我还尝试将http-websockets = true添加到.ini 文件。 It even made uwsgi stop.它甚至使 uwsgi 停止。 So I removed that option.所以我删除了那个选项。

What is wrong here?这里有什么问题?

I get 400 Bad Request for every websocket request.对于每个 websocket 请求,我都会收到 400 个错误请求。 The only data I see is (Opcode -1).我看到的唯一数据是(操作码 -1)。 There is no graphql error message.没有 graphql 错误消息。

You need to change socket type from.sock to net (ip:port).您需要将套接字类型从.sock 更改为 net (ip:port)。 And use this consruction on NGinx "proxy" and server with django.并在 NGinx “代理”和带有 django 的服务器上使用此结构。

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

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