Python Fastapi 发布docker后访问docs提示 Not Found openapi.json

523 阅读1分钟

问题描述:

在本身开发环境访问docs是正常的,制作成镜像后打开提示 Not Found openapi.json。

过程分析:

访问后端的过程使用了 nginx代理到后端,代理是不是使用 /根目录,而是加了前缀,使用的是nginx的rewrite方法。

经过官方文档查阅发现openapi.json的访问方式不可以这么做

image.png

解决方案: 官方有root_path去除代理前缀名。 具体操作: 去除nginx的rewrite,并在FastApi的实例添加代理的前缀 假设nginx代理的是 location /ai 则写入 root_path='/api'

api = FastAPI(root_path='/api')