My server run successful with nginx ,uwsgi ,flask-socketio when used "http" requests.
but when send https requests, the post and get request is successful , the websocket is failed.
The server log as follows:
[pid: 124|app: 0|req: 1/1] 127.0.0.1 () {52 vars in 1101 bytes} [Thu Jan 11 12:23:20 2018] GET /socket.io/?EIO=3&transport=polling&t=M3ZTJdv => generated 168 bytes in 5 msecs (HTTP/1.1 200) 4 headers in 201 bytes (3 switches on core 999)
be563449e0864beca120c9185e770a16: Sending packet OPEN data {'pingInterval': 25000, 'sid': 'be563449e0864beca120c9185e770a16', 'pingTimeout': 60000, 'upgrades': ['websocket']}
connect 1515644621.6367226
c23df10e404a4da5b1ca15208100b33d: Sending packet MESSAGE data 2["my response",{"time":1515644621.6367643}]
be563449e0864beca120c9185e770a16: Sending packet MESSAGE data 2["my response",{"time":1515644621.6367643}]
be563449e0864beca120c9185e770a16: Sending packet MESSAGE data 0
be563449e0864beca120c9185e770a16: Received packet MESSAGE data 2["autolayout_boudingbox",{"version":"1.0.2","id":"tmpl-0000","runCount":20,"targetScore":80,"palette":[],"pages":[],"reqId":"2554124011028842"}]
be563449e0864beca120c9185e770a16: Sending packet MESSAGE data 2["autolayout_boudingbox","{\"runCount\": 20, \"version\": \"1.0.2\", \"palette\": [],"]
[pid: 124|app: 0|req: 7/4] 127.0.0.1 () {56 vars in 1237 bytes} [Thu Jan 11 12:23:52 2018] POST /socket.io/?EIO=3&transport=polling&t=M3ZTRnR&sid=be563449e0864beca120c9185e770a16 => generated 2 bytes in 13108 msecs (HTTP/1.1 200) 3 headers in 138 bytes (3 switches on core 996)
[pid: 124|app: 0|req: 7/5] 127.0.0.1 () {54 vars in 1203 bytes} [Thu Jan 11 12:23:20 2018] GET /socket.io/?EIO=3&transport=websocket&sid=c23df10e404a4da5b1ca15208100b33d => generated 851 bytes in 44714 msecs (HTTP/1.1 200) 4 headers in 176 bytes (3 switches on core 998)
[pid: 124|app: 0|req: 7/6] 127.0.0.1 () {52 vars in 1175 bytes} [Thu Jan 11 12:23:41 2018] GET /socket.io/?EIO=3&transport=polling&t=M3ZTPBT&sid=be563449e0864beca120c9185e770a16 => generated 851 bytes in 23765 msecs (HTTP/1.1 200) 4 headers in 176 bytes (3 switches on core 999)
be563449e0864beca120c9185e770a16: Received packet PING data None
be563449e0864beca120c9185e770a16: Sending packet PONG data None
[pid: 124|app: 0|req: 9/7] 127.0.0.1 () {56 vars in 1235 bytes} [Thu Jan 11 12:24:06 2018] POST /socket.io/?EIO=3&transport=polling&t=M3ZTVI8&sid=be563449e0864beca120c9185e770a16 => generated 2 bytes in 0 msecs (HTTP/1.1 200) 3 headers in 138 bytes (3 switches on core 998)
[pid: 124|app: 0|req: 9/8] 127.0.0.1 () {54 vars in 1203 bytes} [Thu Jan 11 12:23:41 2018] GET /socket.io/?EIO=3&transport=websocket&sid=be563449e0864beca120c9185e770a16 => generated 4 bytes in 24976 msecs (HTTP/1.1 200) 3 headers in 152 bytes (3 switches on core 997)
be563449e0864beca120c9185e770a16: Sending packet CLOSE data None
[pid: 124|app: 0|req: 9/9] 127.0.0.1 () {52 vars in 1175 bytes} [Thu Jan 11 12:24:05 2018] GET /socket.io/?EIO=3&transport=polling&t=M3ZTV14&sid=be563449e0864beca120c9185e770a16 => generated 11 bytes in 60001 msecs (HTTP/1.1 400) 3 headers in 147 bytes (3 switches on core 999)
Invalid session be563449e0864beca120c9185e770a16
[pid: 124|app: 0|req: 10/10] 127.0.0.1 () {56 vars in 1239 bytes} [Thu Jan 11 12:25:06 2018] POST /socket.io/?EIO=3&transport=polling&t=M3ZTji7&sid=be563449e0864beca120c9185e770a16 => generated 11 bytes in 0 msecs (HTTP/1.1 400) 3 headers in 147 bytes (3 switches on core 999)
the browser error as follow:
websocket.js:112 WebSocket connection to 'wss://opus.arkie.cn:444/socket.io/?EIO=3&transport=websocket&sid=be563449e0864beca120c9185e770a16' failed: Error during WebSocket handshake: Unexpected response code: 200
xhr.onreadystatechange @ polling-xhr.js:252
2polling-xhr.js:264 GET https://opus.arkie.cn:444/socket.io/?EIO=3&transport=polling&t=M3ZTV14&sid=be563449e0864beca120c9185e770a16 504 (Gateway Time-out)
Template-879e2cb3-3b88-4d2a-b95f-29f93821e610:1 Failed to load https://opus.arkie.cn:444/socket.io/?EIO=3&transport=polling&t=M3ZTV14&sid=be563449e0864beca120c9185e770a16: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:8080' is therefore not allowed access. The response had HTTP status code 504.
polling-xhr.js:264 POST https://opus.arkie.cn:444/socket.io/?EIO=3&transport=polling&t=M3ZTji7&sid=be563449e0864beca120c9185e770a16 400 (BAD REQUEST)
my nginx config:
upstream flask_nodes{
server 127.0.0.1:8002;
}
server {
listen 8001;
charset utf-8;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name opus.arkie.cn;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404
include proxy_params;
proxy_pass http://flask_nodes;
proxy_redirect off;
# try_files $uri $uri/ =404;
}
location /socket.io {
proxy_pass http://flask_nodes/socket.io;
proxy_http_version 1.1;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
keepalive_timeout 70;
server_name opus.arkie.cn;
server_tokens off;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location / {
proxy_pass http://127.0.0.1:8001;
#proxy_pass http://flask_nodes;
proxy_set_header Host $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;
}
}
my uwsgi config.ini
root@46c6e3977def:/opus# vi /config/websockets_opus.ini
[uwsgi]
chdir = /opus/flaskServer
wsgi-file = flaskManage.py
callable = app
master = true
http-websockets = true
http = :8002
gevent = 1000
vacuum = true
daemonize = /log/uwsgi.log
pidfile=opus_uwsgi.pid
i haven't config uwsgi openssl ,just uesd nginx proxy_pass .
Any ideas on why this happens and how I could fix it?
The https://opus.arkie.cn:444/socket.io/... URLs are not going through a WebSocket friendly location in your nginx configuration. I think you need to add a /socket.io location block in the SSL side.
nginx in my docker container, docker ps : 0.0.0.0:8001->8001/tcp, 0.0.0.0:444->443/tcp .
the server have function: @app.route('/polls/get_relayout_boudingbox', methods=["POST", "GET"]),
send https://opus.arkie.cn:444/polls/get_relayout_boudingbox is successful.
I found that browsers send multiple requests for different sid, some sid can be successful, and others will have the errors described above.
i find question, 1st emit is sucess after connect, and then 2nd same emit make a mistake.
the error log :
1fafffaf18db4ab78f4601237a9046cd: Client is gone, closing socket
thr browser log:
websocket.js:112 WebSocket connection to 'wss://opus.arkie.cn:444/socket.io/?EIO=3&transport=websocket&sid=4152574690f04306858c359c7a47d944' failed: Error during WebSocket handshake: Unexpected response code: 200
Any ideas on why this happens ?
Yes, this is because the initial connection is made via HTTP. Once that connection is in place, the client attempts to upgrade the connection to WebSocket, and that is the part that you have incorrectly configured in your nginx config file.
Thanks! i modify the nginx config
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
keepalive_timeout 1800;
server_name opus.arkie.cn;
location / {
include proxy_params;
proxy_pass http://flask_nodes;
}
location /socket.io {
include proxy_params;
proxy_pass http://flask_nodes/socket.io;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
then found the question: uWSGI with SSL support to use the websocket handshake API function ,and sloved it by stackoverflow.
finally The service runs successful.
Hey guys i need help, i'm having the same issue but i don't use ssl
If you are using native Javascript WebSocket, try adding /websocket to the end of your websocket's URL, for example:
Change from:
ws://abc.com:9999/echo
to:
ws://abc.com:9999/echo/websocket
websocket
Thanks @viettranme works perfectly for me
how to create ws url ws://localhost/demo
@viettranme
Change from:
ws://abc.com:9999/echo
to:
ws://abc.com:9999/echo/websocket
You're the Man! Thank you!
/websocket
hi, It still didn't work for me
Most helpful comment
If you are using native Javascript WebSocket, try adding /websocket to the end of your websocket's URL, for example:
Change from:
ws://abc.com:9999/echoto:
ws://abc.com:9999/echo/websocket