我希望将Flask-SocketIO集成到我的Flask项目中。我的应用程序在Nginx反向代理后运行:
location /plivo_api{
rewrite ^/plivo_api(.*) /$1 break;
proxy_pass http://127.0.0.1:8090;
}
我理解所有在/plivo_api接收到的流量都将被重写为端口8090的"/"。这部分工作得很好。
问题开始出现当我想连接套接字时。直接连接套接字没有问题。
# all those examples work
# from localhost
var socket = io.connect()
var socket = io.connect('http://localhost:8090/')
# running the app out of the reverse proxy
var socket = io.connect('http://my_server:8090/')
但是通过Nginx我无法连接。
# Bad Gateway
var socket = io.connect('http://my_server/plivo_api')
问题是:我连接到我的socketio应用程序是否有什么遗漏,或者需要在Nginx配置中添加额外内容?
集成了socketio的flask应用程序代码如下:
# this code work well, the flask app and socket io
# problem must be in Ngin settings.
The flask app code with socketio integration looks like
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
HOST = '127.0.0.1'
PORT = 8090
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['DEBUG'] = True
app.config['SERVER_NAME'] = f'{HOST}:{PORT}'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
socketio.run(app, port=PORT, host=HOST)