尝试通过HTTPS传递swagger.json时出现"没有规范提供"错误

uko*_*ath 8 swagger flask-restplus

如果我尝试通过HTTPS使用Flask RestPlus提供Swagger UI,我只会在根URL上看到"No spec provided"错误消息,并且从不加载完整的Swagger UI.但是,如果我访问API端点,则会按预期返回响应.

查看错误页面的源HTML,我注意到它swagger.json是从而http://myhost/不是https://myhost/

在restplus Github问题上发现了完全相同的问题

我已经用该页面上提到的猴子补丁暂时解决了我的问题.Swagger UI加载,并查看我看到swagger.json确实从中获取的HTML源代码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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Yus*_*suf 5

我已经用下面的方法让它工作了。您可以在下面的链接中查看稳定的示例。

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)
Run Code Online (Sandbox Code Playgroud)


小智 0

我不确定这是否完全安全,但以下是我在 Nginx 中修复它的方法:

sub_filter "http://$host/" "https://$host/";
sub_filter_once off;
proxy_redirect    off;
Run Code Online (Sandbox Code Playgroud)

我正在 Nginx 上卸载 SSL,这对我来说没有任何问题。它还消除了对应用程序代码进行猴子修补的需要。

您从flask-restplus问题中列出的方法肯定被认为是不安全的:

Please keep in mind that it is a security issue to use such a
middleware in a non-proxy setup because it will blindly trust
the incoming headers which might be forged by malicious clients.
Run Code Online (Sandbox Code Playgroud)