我写了一个使用flask-socketio的Flask应用程序。Flask应用程序在8000端口上运行,客户端应用程序独立地运行在3000端口上(react-webpack)。在开发模式下(由Flask提供的Web服务器),它运行得非常顺畅。然而,在尝试使用uwsgi运行时,我遇到了问题。这些问题和配置将在下面详细说明。
wsgi.py文件内容不变。
请注意,在这两个例子中,http请求(由应用程序提供)运行良好,只有套接字调用出现问题。
from cloud_app import app, sock
if __name__ == "__main__":
sock.run(app,host='0.0.0.0', debug=True, port=8000)
__init__.py (保持不变)
from flask import Flask
from flask_socketio import SocketIO
import secrets
app = Flask(__name__, static_url_path='/static')
app.secret_key = secrets.secret_key
sock = SocketIO(app)
from cloud_app import routes
routes.py(只是移除了实际逻辑而保持不变)
...
from flask_cors import CORS
cors = CORS(app, resources={r"/*": {"origins": "*"}}, headers=['Content-Type'], expose_headers=['Access-Control-Allow-Origin'], supports_credentials=True)
@app.route('/example')
def example():
return 'example'
@sock.on('connect', namespace='/example')
def handle_example_connect():
sock.emit('example', 'Connected!\nAwaiting commands...\n', namespace='/example')
...
首次配置
从Flask-SocketIO和uWSGI文档翻译为ini文件:
[uwsgi]
module = wsgi:app
master = true
processes = 5
buffer-size=32768
http-websockets = true
http = :8000
gevent = 1000
这里不需要nginx配置,因为webpack会提供服务,而ini文件已经配置为直接响应'http=:port'的http请求。
控制台有时候会在routes.py中的connect事件中打印连接成功的信息"Connected! Awaiting commands...",但也可能会出现以下错误:
POST http://localhost:8000/socket.io/?EIO=3&transport=polling&t=MgTjSL-&sid=5bf4758a09034805b1213fec92620e39 400 (BAD REQUEST)
GET http://localhost:8000/socket.io/?EIO=3&transport=polling&t=MgTjSMG&sid=5bf4758a09034805b1213fec92620e39 400 (BAD REQUEST)
websocket.js:112 WebSocket connection to 'ws://localhost:8000/socket.io/?EIO=3&transport=websocket&sid=5bf4758a09034805b1213fec92620e39' failed: Error during WebSocket handshake: Unexpected response code: 400
UWSGI进程输出:
...
[pid: 9402|app: 0|req: 16/33] 127.0.0.1 () {44 vars in 1316 bytes} [Thu May 9 13:55:41 2019] POST /socket.io/?EIO=3&transport=polling&t=MgTl93y&sid=b208e874c0e64330bdde35ae1773b4e0 => generated 2 bytes in 0 msecs (HTTP/1.1 200) 3 headers in 137 bytes (3 switches on core 996)
[pid: 9402|app: 0|req: 17/34] 127.0.0.1 () {40 vars in 1255 bytes} [Thu May 9 13:55:41 2019] GET /socket.io/?EIO=3&transport=polling&t=MgTl94Q&sid=b208e874c0e64330bdde35ae1773b4e0 => generated 12 bytes in 0 msecs (HTTP/1.1 200) 3 headers in 151 bytes (3 switches on core 996)
...
[pid: 9402|app: 0|req: 27/48] 127.0.0.1 () {44 vars in 1316 bytes} [Thu May 9 13:56:57 2019] POST /socket.io/?EIO=3&transport=polling&t=MgTlRbG&sid=5c4c38f18f6b47798978440edd181512 => generated 2 bytes in 0 msecs (HTTP/1.1 200) 3 headers in 137 bytes (3 switches on core 998)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask_socketio/__init__.py", line 43, in __call__
start_response)
File "/usr/local/lib/python2.7/dist-packages/engineio/middleware.py", line 47, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/socketio/server.py", line 360, in handle_request
return self.eio.handle_request(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/engineio/server.py", line 322, in handle_request
start_response(r['status'], r['headers'] + cors_headers)
IOError: headers already sent
...
第二个配置
摘自这个问题的回答。 ini文件
[uwsgi]
module = wsgi:app
master = true
processes = 5
buffer-size=32768
http-websockets = true
socket = example_app.sock
chmod-socket = 666
vaccum = true
die-on-term = true
nginx服务器
server {
listen 8000;
location /{
include uwsgi_params;
uwsgi_pass unix:/path/to/app/example_app.sock;
}
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://unix:/path/to/app/example_app.sock;
}
}
之前未注释的选项现在被注释掉了
错误
控制台:
polling-xhr.js:263 GET http://localhost:8000/socket.io/?EIO=3&transport=polling&t=MgTotN9 502 (Bad Gateway)
Access to XMLHttpRequest at 'http://localhost:8000/socket.io/?EIO=3&transport=polling&t=MgTotN9' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
nginx错误日志(/var/log/nginx/error_log)
2019/05/09 14:16:35 [error] 11338#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /socket.io/?EIO=3&transport=polling&t=MgTpw36 HTTP/1.1", upstream: "http://unix:/path/to/app/example_app.sock:/socket.io/?EIO=3&transport=polling&t=MgTpw36", host: "localhost:8000", referrer: "http://localhost:3000/home"
请注意,在这两个例子中,http请求(由应用程序提供)运行良好,只有套接字调用出现问题。