get_swagger_ui_html does not automatically prepend root_path to openapi_url
Description:
When running FastAPI behind a proxy (e.g., Nginx) with a path prefix like /inference/newmodel2-23/, we use the root_path parameter and X-Forwarded-Prefix headers. While the internal /docs handler works perfectly, manual implementations using get_swagger_ui_html result in a 404 for the openapi.json file because the proxy prefix is missing from the generated HTML.
Internal FastAPI Context
Looking at the FastAPI source:
https://github.com/fastapi/fastapi/blob/a456e92a21d5b94a67d6d0fd070218df09444443/fastapi/applications.py#L1093-L1107
Your source code
https://github.com/SeldonIO/MLServer/blob/a325e523b5a580d49aeb06e19e73e4a90176b63b/mlserver/rest/endpoints.py#L44-L47
Expected Behavior
get_swagger_ui_html should ideally have an optional request parameter or a way to automatically detect and prepend the root_path so that documentation ui and routes work when behind a proxy.
Environment (docker containers)
- MLflow image: ghcr.io/mlflow/mlflow:v3.8.1
- Proxy: Nginx
Example Nginx Config
server {
listen 8983;
server_name localhost;
location /inference/newmodel2-23 {
proxy_pass http://fastapi:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Prefix /inference/newmodel2-23;
}
}
I am new to open source and if this is really the issue and I am not missing anything somehow, then I would like to contribute. FastApi docs that mention what I have written about above: https://fastapi.tiangolo.com/advanced/behind-a-proxy/#proxy-with-a-stripped-path-prefix