Flask-SocketIO和400 Bad Request

问题描述 投票:0回答:1

我正在运行带有socketio的Flask应用程序来处理通知。 Flask应用程序正在端口5000监听,客户端在8080。

js客户端总是收到此错误:

VM15520:1 GET http://localhost:5000/socket.io/?EIO=3&transport=polling&t=Mb2_LpO 400 (Bad Request)
Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=3&transport=polling&t=Mb2_LpO' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我实际上用gunicorn开始我的应用程序如下:

gunicorn --workers=1 --worker-class eventlet --certfile=keys/key.crt --keyfile=keys/key.key --bind 0.0.0.0:5000 myapp.run:app

这是我的run.py:

import eventlet
from myapp import create_app
eventlet.monkey_patch()
app, celery = create_app('config_prod.py')

我也在我的应用工厂中使用CORS(app)。

我也尝试在我的一个蓝图中添加它:

@api.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', 'http://localhost:8080')
    response.headers.add('Access-Control-Allow-Headers',
                         'Origin, X-Requested-With, Content-Type, Accept, Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    response.headers.add('Access-Control-Allow-Credentials', 'false')
    return response

我使用nginx作为反向代理,所以我尝试添加我在flask-socketio的文档中看到的相应配置:

location /socket.io {
    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_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass https://my_backend_host/socket.io;
}

怎么了?谢谢!

python flask socket.io cors flask-socketio
1个回答
0
投票

问题是我用http而不是https添加原始主机。一切都很好。

© www.soinside.com 2019 - 2024. All rights reserved.