看不到带有 2 main.py 和 1 个 nginx 反向代理的 fastAPI 文档
can't see fastAPI documentation with 2 main.py and 1 nginx reverse proxy
我将 fastAPI 与 nginx 一起用作反向代理。我将 APIs 分成 2 个不同的 main.py,具有不同的端点:
- main.py -> API 的主要 location/endopoint:
/
(端口 5010)
- main_slow.py -> API 的主要 location/endopoint:
/slow_api
(端口 5011)
到 运行 它们在不同的端口 (5010,5011)。这是因为一个 API 非常慢并且在系列中请求 APIs 我需要一个与其他的分开(在 main_slow.py 中)。使用 nginx 作为反向代理,我可以在单个端口 (8000) 下使用它们自己的端点调用 APIs,然后 nginx 将负责将它们传递到 fastAPI 的正确端口。
一切正常,唯一的问题是我无法在 /docs 中看到所有 API 文档,只能看到第一个 main.py(位置 /
)的端点。
在 /nginx/conf.d/
我有 py_api.conf
并且我这样配置它:
server {
listen 8000;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Origin,X-Auth-Token';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = OPTIONS ) {
return 200;
}
proxy_pass http://localhost:5010;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd-pyapi; #For Basic Auth
}
location /slow_api{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Origin,X-Auth-Token';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = OPTIONS ) {
return 200;
}
proxy_pass http://localhost:5011;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd-pyapi; #For Basic Auth
}
}
我是不是做错了什么才能看到这两个位置的文档?或者我必须对 python 和 fastAPI 做些什么?
已解决
要查看所有两个文档(目前在两个不同的路径中),我在我的 fastAPI 项目中添加 main_slow.py
app = FastAPI( title='slow API',
docs_url='/slow_api/docs',
redoc_url='/slow_api/redoc',
openapi_url='/slow_api/openapi.json')
仅
app = FastAPI()
您必须更改位置块的顺序。 Nginx 在第一次成功匹配时终止位置匹配。在您的情况下,对 /slow_api
的所有请求都与第一个位置块匹配,并且从不检查第二个块。
你可以查看官方文档了解更多详情。
您可能想看看这些:
您想在同一路径上查看 API 两者的文档吗?然后看看Behind a Proxy: Additional Servers
如果您可以为每个 API 文档设置不同的路由,那么您可以通过 docs_url, redoc_url 和 openapi_url 参数到 FastAPI class 并处理我猜想的情况。看看 this.
我将 fastAPI 与 nginx 一起用作反向代理。我将 APIs 分成 2 个不同的 main.py,具有不同的端点:
- main.py -> API 的主要 location/endopoint:
/
(端口 5010) - main_slow.py -> API 的主要 location/endopoint:
/slow_api
(端口 5011)
到 运行 它们在不同的端口 (5010,5011)。这是因为一个 API 非常慢并且在系列中请求 APIs 我需要一个与其他的分开(在 main_slow.py 中)。使用 nginx 作为反向代理,我可以在单个端口 (8000) 下使用它们自己的端点调用 APIs,然后 nginx 将负责将它们传递到 fastAPI 的正确端口。
一切正常,唯一的问题是我无法在 /docs 中看到所有 API 文档,只能看到第一个 main.py(位置 /
)的端点。
在 /nginx/conf.d/
我有 py_api.conf
并且我这样配置它:
server {
listen 8000;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Origin,X-Auth-Token';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = OPTIONS ) {
return 200;
}
proxy_pass http://localhost:5010;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd-pyapi; #For Basic Auth
}
location /slow_api{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Origin,X-Auth-Token';
add_header 'Access-Control-Allow-Credentials' 'true';
if ($request_method = OPTIONS ) {
return 200;
}
proxy_pass http://localhost:5011;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file /etc/nginx/.htpasswd-pyapi; #For Basic Auth
}
}
我是不是做错了什么才能看到这两个位置的文档?或者我必须对 python 和 fastAPI 做些什么?
已解决
要查看所有两个文档(目前在两个不同的路径中),我在我的 fastAPI 项目中添加 main_slow.py
app = FastAPI( title='slow API',
docs_url='/slow_api/docs',
redoc_url='/slow_api/redoc',
openapi_url='/slow_api/openapi.json')
仅
app = FastAPI()
您必须更改位置块的顺序。 Nginx 在第一次成功匹配时终止位置匹配。在您的情况下,对 /slow_api
的所有请求都与第一个位置块匹配,并且从不检查第二个块。
你可以查看官方文档了解更多详情。
您可能想看看这些:
您想在同一路径上查看 API 两者的文档吗?然后看看Behind a Proxy: Additional Servers
如果您可以为每个 API 文档设置不同的路由,那么您可以通过 docs_url, redoc_url 和 openapi_url 参数到 FastAPI class 并处理我猜想的情况。看看 this.