Skip to content

get_swagger_ui_html does not automatically prepend root_path to openapi_url #2344

@aliisayev008gemini-cloud

Description

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

async def docs(self) -> HTMLResponse:
openapi_url = "/v2/docs/dataplane.json"
title = "MLServer API Docs"
return get_swagger_ui_html(openapi_url=openapi_url, title=title)

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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions