Uwsgi和Nginx上的SocketIO烧瓶

时间:2017-06-14 07:22:13

标签: nginx websocket socket.io uwsgi flask-socketio

我正在使用Flask应用程序,该应用程序使用WebSockets与前端进行通信。它托管在nginx后面的Amazon EC2和uwsgi上的serverd。

这是uwsgi配置,我用来提供app:

[uwsgi]
plugins=python3,logfile
chdir=/srv/myapp/
master=true
home=/srv/myapp/.venv
module=application
callable=flask_app
uid=uwsgi
gid=myapp
socket=/srv/myapp/uwsgi.sock
chown-socket=uwsgi:myapp
chmod-socket=660
logto = /srv/myapp/logs/uwsgi.log
for-readline = /srv/myapp/.vars
  env = %(_)
endfor =

摘自nginx配置,其中包含socketio端点:

location /socket.io/ {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass http://unix:///srv/myapp/uwsgi.sock;
}

没有websockets一切正常。本地(我正在使用Windows)一切都很完美 - 我只是在客户端添加transport: ['websockets', 'polling']以确保选择正确的协议。

本地我按照Flask-SocketIO文档中的建议运行它,并且我安装了eventlet(我不知道为什么但是在Windows x64 gevent包装器中与{{1}一起工作很糟糕} dev server)。

部署应用时,我在浏览器中只看到错误Werkzeug。在服务器端,我在uwsgi日志中跟随:websocket.js:112 WebSocket connection to 'ws://myapp.com/socket.io/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 502。增加缓冲区大小不会改变任何内容。

在nginx日志中,我有:invalid request block size: 21573 (max 4096)...skip

我尝试将这些行添加到uwsgi.ini:

*413 upstream prematurely closed connection while reading response header from upstream, client: 171.6.248.10, server: localhost,, request: "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1", upstream: "http://unix:///srv/myapp/uwsgi.sock:/socket.io/?EIO=3&transport=websocket", host: "myapp.com"

没有任何成功

它是什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

It is not actual answer to my question, just a bit of information

My application uses Python 3 and RedHat provides Python3.4 from EPEL. Nevertheless UWSGI can works with WebSockets only using gevent. So, as @Miguel mentioned there is only one way - to use long-polling.

Solution

Finally I changed appserver to Gunicorn that served WebSockets out of box.