【发布时间】:2021-09-07 20:17:24
【问题描述】:
我在 tornado WSGI 容器中部署了一个烧瓶应用程序。龙卷风服务器托管在http://localhost:5000。整个事情都是使用 Nginx 反向代理部署的。烧瓶通过 CORS 将其端点提供给 tornado 服务器:
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
app.config['CORS_HEADERS'] = ['Content-Type', 'Authorization']
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
flask应用的部署方式如下:
flask_app = tornado.wsgi.WSGIContainer(app)
tornado_app = tornado.web.Application(
[
# tornado specific handlers,
(r"/(.*)", tornado.web.FallbackHandler, dict(fallback=flask_app))
],
log_function=log_function,
websocket_max_message_size=100 * 1024 * 1024)
http_server = tornado.httpserver.HTTPServer(
tornado_app,
max_buffer_size=1024 ** 3,
)
这是我的 Nginx 配置:
upstream frontend {
server localhost:5000;
}
gzip on;
gzip_types text/plain text/css text/xml
application/x-javascript application/xml
text/javascript;
proxy_next_upstream error;
server {
listen <server_ip>:80;
server_name <server_hostname>;
auth_basic "Administrator's Area";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://frontend/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Scheme $scheme;
}
}
当我使用 Nginx 反向代理时,我收到所有烧瓶端点的 CORS: Request did not succeed 错误。没有它,localhost:5000 的龙卷风服务器将接受所有烧瓶路由。有时,它会自行工作,有时则不会。大多数情况下,是后者。我也尝试在 Nginx 配置中设置特定于 cors 的标头,但它不起作用。
【问题讨论】:
标签: python-3.x nginx flask tornado flask-cors