如果我尝试使用 Flask RestPlus 通过 HTTPS 交付 Swagger UI,我只会在根 URL 处看到“未提供规范”错误消息,并且完整的 Swagger UI 永远不会加载。但是,如果我访问 API 端点,它们会按预期返回响应。
查看错误页面的源 HTML,我注意到swagger.json
正在从以下位置获取http://myhost/
而不是https://myhost/
我发现了完全相同的问题其余的Github问题 https://github.com/noirbizarre/flask-restplus/issues/54
我已经暂时解决了我的问题猴子补丁 https://github.com/noirbizarre/flask-restplus/issues/54#issuecomment-136774846该页面上提到。 Swagger UI 加载,查看 HTML 源代码我发现swagger.json
确实是取自https://myhost
.
为什么会发生这种情况,以及如何在不进行猴子修补的情况下修复它?
HTTPS 由 Cloudflare 的“灵活”HTTPS 服务提供。
我的应用程序位于 Nginx 后面,它是这样配置的,据我所知,没有造成任何问题:
...
http {
...
server {
location / {
charset UTF-8;
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
charset UTF-8;
proxy_intercept_errors on;
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;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:5000;
}
}
}
我已经用下面的方法让它工作了。您可以在下面的链接中查看稳定的示例。
http://flask-restplus.readthedocs.io/en/stable/example.html http://flask-restplus.readthedocs.io/en/stable/example.html
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)