簡體   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