首页 > 解决方案 > 无法让 Flask-SocketIO、uWSGI 和 NGINX 工作

问题描述

嗨,伙计们,我已经尝试解决这个问题几个星期了,但我完全不知所措。

我有一个烧瓶应用程序,它作为我的应用程序的 REST API 成功运行。我决定将 Web 套接字添加到我的 API 中,以便在我的数据库更新或某些事件时触发我的应用程序中的更新。

我添加到烧瓶应用程序中的内容:

from flask_socketio import SocketIO, emit, send
socketio = SocketIO(app, cors_allowed_origins="*")

uwsgi.ini 文件:

[uwsgi]
module = wsgi:app

master = true
processes = 5
http-websockets = true <--- added this
socket = fzInternal_API.sock
chmod-socket = 660
vacuum = true

die-on-term = true
logto = /home/brett/fzInternal_API/fzInternal_API.log

我的 wsgi.py 文件:

from fzInternal_API import app

if __name__ == "__main__":
    #app.run()
    socketio.run(app, port=5000, host='0.0.0.0')

我在站点中启用的 NGINX 配置文件:


server {
    listen 5000;
    server_name 10.10.10.3:5000;


    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/brett/fzInternal_API/fzInternal_API.sock;
        uwsgi_ignore_client_abort on;
    }

    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        add_header "Access-Control-Allow-Origin" "http://10.10.10.3" always;
        add_header "Access-Control-Allow-Credentials" "true" always;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://10.10.10.3:5000;
        proxy_hide_header Access-Control-Allow-Origin;
        proxy_hide_header Access-Control-Allow-Credentials;
        proxy_hide_header Access-Control-Allow-Headers;
    }

}

我的 Systemd 服务文件来启动它:

[Unit]
Description=uWSGI instance to serve fzInternal_API
After=network.target

[Service]
User=brett
Group=www-data
WorkingDirectory=/home/brett/fzInternal_API
Environment="PATH=/home/brett/fzInternal_API/bin"
ExecStart=/home/brett/fzInternal_API/bin/uwsgi --ini /home/brett/fzInternal_API/fzInternal_API.ini

[Install]
WantedBy=multi-user.target

现在我最初遇到了一堆我无法弄清楚的 CORS 错误,但通过调整 NGINX 文件以某种方式解决了它们(我在烧瓶配置中所做的任何事情都没有工作)但是现在当我尝试使用一个简单的连接到 websocket 服务器时html 文件(托管在另一个虚拟服务器实例中的同一台服务器上)我只是交替出现 500(内部服务器错误)和 400(错误请求)错误。

日志中似乎没有任何指示,所以我不知所措。顺便说一句,正常的烧瓶终点仍然可以正常工作。

标签: nginxflaskwebsocketuwsgiflask-socketio

解决方案


推荐阅读