Flask-SocketIO: IOError: unable to complete websocket handshake while running in uwsgi+Nginx
I have been planning to deploy a flask-SocketIO application into production using uwsgi server behind Nginx
Versions including relevant dependencies of my virtual environment:-
OS:- Ubuntu 16.04.3 LTS
uWsgi:- 2.0.15
nginx:- 1.12.2
python:- 2.7.12
Falsk:- 0.12.2
Flask-SocketIO:- 2.9.2
gevent:- 1.2.2
I am intended to run the uwsgi server through systemd and load the uwsgi configurations from a .ini file.
Here’s my wsgi.py file:
from server import app
if __name__ == "__main__":
app.run()
Here’s the configuration file myproject.ini for uwsgi
[uwsgi]
module = wsgi:app
http = :5000
gevent = 1000
http-websockets = true
master = true
processes = 5
socket = myproject.sock
chmod-socket = 660
vacuum = true
die-on-term = true
Here’s the myproject.service file in /etc/systemd/system
[Unit]
Description=uWSGI instance to serve myproject
After=network.target
[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/myproject
Environment="PATH=/home/user/myproject/virtual_frame/bin"
ExecStart=/home/user/myproject/virtual_frame/bin/uwsgi --ini myproject.ini
[Install]
WantedBy=multi-user.target
And, finally my nginx configuration file:
server {
listen 80;
server_name 10.11.201.82:5000;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/user/myproject/myproject.sock;
uwsgi_ignore_client_abort on;
}
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://10.11.201.82:5000/socket.io;
}
}
Errors:-
Actually I’m getting different errors from several times.
1) IOError: unable to complete websocket handshake
Here’s my project status while running:_
user@user-HVM-domU:~/myproject$ sudo journalctl -u myproject -n 20
-- Logs begin at শনি 2018-02-03 09:52:05 +06, end at রবি 2018-02-04 15:27:48 +06. --
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/flask/app.py", line 1997, i
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: return self.wsgi_app(environ, start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/flask_socketio/__init__.py"
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/engineio/middleware.py", li
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: return self.engineio_app.handle_request(environ, start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/socketio/server.py", line 3
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: return self.eio.handle_request(environ, start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/engineio/server.py", line 2
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: environ, start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/engineio/socket.py", line 8
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/engineio/socket.py", line 1
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: return ws(environ, start_response)
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: File "/home/user/myproject/virtual_frame/local/lib/python2.7/site-packages/engineio/async_gevent_uwsgi
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: uwsgi.websocket_handshake()
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: IOError: unable to complete websocket handshake
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: [pid: 23604|app: 0|req: 21/79] 10.11.201.82 () {50 vars in 1028 bytes} [Sun Feb 4 15:27:39 2018] GET /s
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: [pid: 23604|app: 0|req: 22/80] 10.11.201.82 () {38 vars in 784 bytes} [Sun Feb 4 15:27:39 2018] GET /so
ফেবà§à¦°à§ 04 15:27:39 user-HVM-domU uwsgi[23592]: [pid: 23604|app: 0|req: 23/81] 10.11.201.82 () {42 vars in 845 bytes} [Sun Feb 4 15:27:39 2018]
2) IOError: write error: uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.
ফেবà§à¦°à§ 04 15:16:53 user-HVM-domU uwsgi[23421]: [pid: 23433|app: 0|req: 52/94] 10.11.201.82 () {42 vars in 845 bytes} [Sun Feb 4 15:16:38 2018] POST /s
ফেবà§à¦°à§ 04 15:16:53 user-HVM-domU uwsgi[23421]: [pid: 23433|app: 0|req: 52/95] 10.11.201.82 () {38 vars in 783 bytes} [Sun Feb 4 15:16:30 2018] GET /so
ফেবà§à¦°à§ 04 15:16:55 user-HVM-domU uwsgi[23421]: [pid: 23433|app: 0|req: 54/96] 10.11.201.82 () {42 vars in 843 bytes} [Sun Feb 4 15:16:55 2018] POST /s
ফেবà§à¦°à§ 04 15:16:55 user-HVM-domU uwsgi[23421]: [pid: 23433|app: 0|req: 54/97] 10.11.201.82 () {38 vars in 783 bytes} [Sun Feb 4 15:16:53 2018] GET /so
ফেবà§à¦°à§ 04 15:17:08 user-HVM-domU uwsgi[23421]: Sun Feb 4 15:17:08 2018 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line
ফেবà§à¦°à§ 04 15:17:08 user-HVM-domU uwsgi[23421]: IOError: write error
ফেবà§à¦°à§ 04 15:17:08 user-HVM-domU uwsgi[23421]: [pid: 23433|app: 0|req: 55/98] 10.11.201.82 () {44 vars in 867 bytes} [Sun Feb 4 15:16:08 2018] GET /so
3) Nginx error log
Last 20 line of /var/log/nginx/error.log
2018/02/04 15:24:42 [error] 23563#23563: *109 upstream prematurely closed connection while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "GET /socket.io/?EIO=3&transport=websocket&sid=b95aa3f7a7374706a7d09d857c1e69fe HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=websocket&sid=b95aa3f7a7374706a7d09d857c1e69fe", host: "10.11.201.82"
2018/02/04 15:25:40 [error] 23560#23560: *83 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "GET /socket.io/?EIO=3&transport=polling&t=1517736280486-2&sid=998d555f29cc49f3949bf080b5c2820c HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=polling&t=1517736280486-2&sid=998d555f29cc49f3949bf080b5c2820c", host: "10.11.201.82", referrer: "http://10.11.201.82/"
2018/02/04 15:25:49 [error] 23563#23563: *98 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "GET /socket.io/?EIO=3&transport=polling&t=1517736289238-36&sid=6535da975fee43bdb49be5f061e04522 HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=polling&t=1517736289238-36&sid=6535da975fee43bdb49be5f061e04522", host: "10.11.201.82", referrer: "http://10.11.201.82/"
2018/02/04 15:25:50 [error] 23563#23563: *138 upstream prematurely closed connection while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "GET /socket.io/?EIO=3&transport=polling&t=1517736290606-40&sid=c13d0711b34b49cb8d5d973642369c33 HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=polling&t=1517736290606-40&sid=c13d0711b34b49cb8d5d973642369c33", host: "10.11.201.82", referrer: "http://10.11.201.82/"
2018/02/04 15:25:54 [error] 23563#23563: *143 upstream prematurely closed connection while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "GET /socket.io/?EIO=3&transport=polling&t=1517736294273-52&sid=aec315362ea644fe93035f7d8c5519a6 HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=polling&t=1517736294273-52&sid=aec315362ea644fe93035f7d8c5519a6", host: "10.11.201.82", referrer: "http://10.11.201.82/"
2018/02/04 15:25:56 [error] 23560#23560: *83 upstream prematurely closed connection while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "GET /socket.io/?EIO=3&transport=polling&t=1517736346555-72&sid=933777c0ced64dc4a459b1ebe33a0d21 HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=polling&t=1517736346555-72&sid=933777c0ced64dc4a459b1ebe33a0d21", host: "10.11.201.82", referrer: "http://10.11.201.82/"
2018/02/04 15:25:56 [error] 23560#23560: *83 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.11.201.82, server: 10.11.201.82, request: "POST /socket.io/?EIO=3&transport=polling&t=1517736356497-73&sid=933777c0ced64dc4a459b1ebe33a0d21 HTTP/1.1", upstream: "http://10.11.201.82:5000/socket.io/?EIO=3&transport=polling&t=1517736356497-73&sid=933777c0ced64dc4a459b1ebe33a0d21", host: "10.11.201.82", referrer: "http://10.11.201.82/
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 42 (18 by maintainers)
Have you seen the documentation regarding deployment with uwsgi? I suggest you start from the configuration from there. In particular, you have to use one worker.
To use multiple workers with SocketIO the set up is more complicated. The workers must all be standalone uwsgi instances with one worker, and nginx should be configured to load balance across all these workers. Enabling multiple workers in uwsgi is not supported because this protocol requires sticky sessions, which nginx provides.
@evangelos-dimitriadis It’s the same error: “you need to build uWSGI with SSL support to use the websocket handshake api function !!!”. So do that, and come back if you still have issues with a correctly built uWSGI.
@felocru Oh, I missed that message above. In this context “SSL support” means the SSL libraries, which uWSGI must be using to handle some parts of the websocket handshake. It does not mean that WebSocket only works on secure HTTP, those are two completely different things. But yes, try rebuilding uWSGI after installing SSL libs.