Search code examples
nginxflasksocket.iouwsginginx-reverse-proxy

Flask Socket io is inconsistent with connection (uwsgi+nginx)


When I am running my socket on my local machine(development server) it is working perfectly.

app = Flask(__name__)
socketio = SocketIO(app)
if __name__ == '__main__':
    socketio.run(host='0.0.0.0', port=2000, app=app)

Although when I deployed the same on a server using UWSGI and reverse proxy using NGINX, the connection response is returned as BAD REQUEST(400). The connection gets established for sometime though but is very inconsistent.

I checked the error in nginx logs and found out the error "upstream prematurely closed connection while reading response header from upstream". I have been struggling with this error for quite some time. It would be great if someone could help me out of this mess :/

P.S. Tell me if any other kind of logs are required to further investigate


Solution

  • Finally found a solution to my problem. Although it is not very elaborate but it is what it is. Since most of the sample examples had described taking workers as 3, I was deploying mine with the same configuration but that was resulting in prematurely closing the connection, I assume this could have been because the server was not capable of running multiple workers together. So what I did was, I reduced the workers to 1 and implemented gevent on top of it. The command looked something like following(using gunicorn):

    gunicorn -k gevent -w 1 module:app