I am trying to run my flask application with flask-socketio. After changing many configurations, I finally managed to run my flask application with gunicorn, nginx and gevent on ubuntu server on aws. But whenever I try to connect, the connection fails and in error logs I get Invalid session error. I have installed gevent-websockets as well but it also did not work. Here is the error message.
[29/06/2020 03:50:28.388|15022|WARNING|engineio.server |server.py:391 handle_request ]: Invalid session 90ff42cdbd944b07ad2c2f6f484ff5a3
[29/Jun/2020:03:50:28 +0500] 127.0.0.1 "POST /socket.io/?EIO=3&transport=polling&t=NBz7-Tz&sid=90ff42cdbd944b07ad2c2f6f484ff5a3 HTTP/1.1" 400 11 "-" "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
Here is my nginx configuration.
server {
listen 80;
server_name my_app.com;
location / {
proxy_pass "http://localhost:5000";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
fastcgi_read_timeout 300s;
proxy_read_timeout 300;
}
location /static {
alias /opt/deployment/my-api-app/static/;
}
error_log /var/log/nginx/api-error.log;
access_log /var/log/nginx/api-access.log;
location /socket.io {
include /etc/nginx/proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:5000/socket.io;
}
}
These are the running instances.
Update 1
after running the app using gunicorn -k gevent -w 1
I am getting this error:
Traceback (most recent call last):
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/gunicorn/workers/base_async.py", line 55, in handle
self.handle_request(listener_name, req, client, addr)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/gunicorn/workers/ggevent.py", line 143, in handle_request
super().handle_request(listener_name, req, sock, addr)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/gunicorn/workers/base_async.py", line 106, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/flask_socketio/__init__.py", line 46, in __call__
start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/middleware.py", line 60, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/socketio/server.py", line 560, in handle_request
return self.eio.handle_request(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/server.py", line 377, in handle_request
environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/socket.py", line 108, in handle_get_request
start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/socket.py", line 152, in _upgrade_websocket
return ws(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/async_drivers/gevent.py", line 35, in __call__
raise RuntimeError('You need to use the gevent-websocket server. '
Update 2
As per docs
When using gunicorn with the gevent worker and the WebSocket support provided by gevent-websocket, the command that starts the server must be changed to select a custom gevent web server that supports the WebSocket protocol. The modified command is:
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app