【问题标题】:getting 400 Bad Request error frequently when trying to use flask-socket with uwsgi and nginx尝试将flask-socket与uwsgi和nginx一起使用时经常收到400 Bad Request错误
【发布时间】:2019-05-03 12:33:25
【问题描述】:

我有一个烧瓶应用程序在端口 5000 上运行 Flask-SocketIO。

我正在使用 uwsgi 在生产服务器上运行这个应用程序。

这是我的应用程序的 uwsgi .ini 文件:

[uwsgi]
module = server.webserver:app
callable =  app

master = true
processes = 5

http-socket = 0.0.0.0:5000

die-on-term = true
plugin = python35

#chdir = /var/xyz/webapp
wsgi-file = /var/xyz/webapp/server/webserver.py
virtualenv = /opt/venv3
#home = /opt/venv3/bin
gevent = 1000
enable-threads = true

我使用 nginx 作为这个应用程序的反向代理,我的 nginx 服务器块是:

 server {
        #listen 80 default_server;
        #listen [::]:80 default_server;

        client_body_timeout 15s;
        client_header_timeout 15s;

        server_name x.y.z;

        root /var/xyz/webapp;
        index index.html index.htm index.nginx-debian.html;
        location /{
            proxy_pass      http://127.0.0.1:5000;
            proxy_redirect  off;

            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /socket.io {
            include proxy_params;
            proxy_http_version 1.1;
            proxy_buffering off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_pass http://localhost:5000/socket.io;
        }
 }

现在每次客户端尝试连接到套接字时,请求都会经常收到 400 Bad Request Error。但是,如果我从我的 uwsgi .ini 文件中评论这些行:

#master = true
#processes = 5

套接字已连接并正常运行。

【问题讨论】:

    标签: python-3.x nginx flask uwsgi flask-socketio


    【解决方案1】:

    我知道这有点晚了,但我相信这与流程 = 5 部分有关。根据link 的开发人员,Nginx 应该设置为跨套接字服务器对多个进程进行负载平衡。这更多地是为了帮助其他任何偶然发现这一点的人。 Flask-SocketIO imo 很难正确配置。不过,在 Nginx 中对多个服务器进行负载平衡很容易。就像将烧瓶应用程序绑定到新端口(5001、5002 等)并将具有 uWSGI 应用程序的节点作为节点添加到 Nginx 一样简单。

    更多细节可以在link找到,因为他们解释得更好

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 2018-06-09
      • 2019-09-29
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2017-11-06
      • 2021-03-05
      • 2019-04-20
      相关资源
      最近更新 更多