question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Root path is applied 2 times when using root_path

See original GitHub issue

Here is error: image

Python 3.7.8 fastapi 0.63.0

Run configuration: uvicorn.run("app:app", host="0.0.0.0", port=port, reload=True)

I think the error is in the path here is console log: 127.0.0.1:54913 - "GET /api/v1/api/v1/openapi.json HTTP/1.1" 404 Not Found Here is double path: /api/v1/api/v1/

Doc path is wrong http://localhost:8080/docs I expect it to be: http://localhost:8080/api/v1/docs

Here is the test code to reproduce ‘test_app.py’ file:

import uvicorn
import json
from fastapi import FastAPI, APIRouter, Response
from fastapi.responses import RedirectResponse

app = FastAPI(title="Root path test", root_path="/api/v1")

@app.post("/test-call ", tags=["test"])
def ping():
    return Response(
        json.dumps(dict(ping='pong')), 
        headers={'Content-Type':'application/json'})

@app.get("/")
def read_typer():
    return RedirectResponse('/docs')


if __name__ == "__main__":
    uvicorn.run("test_app:app", host="0.0.0.0", port=8080, reload=True)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
chirag4semandexcommented, Sep 1, 2021

This thread gave me some hints. I found one missing link that helped me solved the openapi.json ( not_found ) issue. Here is what worked for me

I am using nginx as proxy server and FastAPI with Uvicorn. FastAPI and Uvicorn both have the root_path property and they both need to be set for nginx proxy to work properly.

For the openapi.json issue, we need to pass servers property in the FastAPI pointing to the proxy URL.

Here is the sample code for app

from pydantic import BaseSettings

class Settings(BaseSettings):
    app_dir: Path = Path(__file__).parents[1]
    base_path: str = "/app"


settings = Settings()


def create_app():
   app = FastAPI(
        title="MY_APP",
        servers=[{"url": settings.base_path}],
        root_path=settings.base_path,
    )
    return app


if __name__ == "__main__":
    uvicorn.run("app:create_app", host="0.0.0.0", port=8000, root_path=settings.base_path)

Here is a sample code for nginx setup

server {
    listen       443 ssl;
    server_name  localhost;
    ssl_certificate <YOUR_CERT_LOC>;
    ssl_certificate_key <YOUR_KEY_LOC>;

    location /app {
        rewrite ^/app/(.*)$ /$1 break;
        proxy_pass http://<APP_SERVICE_NAME>:<APP_LOCAL_PORT>/app;
    }
}

8reactions
intangircommented, May 10, 2021

im seeing similar irritating issues with both lambdas served over an api gatewith with a prefix path

and locally, using both root_path and a router with a prefix

reading the EXACT DOCS from here https://fastapi.tiangolo.com/advanced/behind-a-proxy/ and setting up the root_path…

also using router it isn’t working…

it always just serves things up as if the entire path is always the entire path regardless…

its incredibly infuriating reading doc after doc after doc and following all the the instructures verbatim and not a damn new thing making a damn bit of difference in how its served…

then finding people with the exact same issue being told ‘expected behavior not a bug’

then what are these worthless features for? how should they work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Application Root Path of Spring Boot application
I need to upload a file to a directory e.g. uploads created in application main directory.
Read more >
Path resolution in Quarkus
root -path , matching the original behavior. You can use an absolute path, e.g. /q , to serve non-application endpoints from the absolute...
Read more >
Getting the Web Root Path and the Content Root Path in ASP ...
The web root path is the absolute path to the directory that contains the web-servable application content files. You can use either path...
Read more >
Path.GetPathRoot Method (System.IO) | Microsoft Learn
Gets the root directory information from the path contained in the specified string. ... The following example demonstrates a use of the GetPathRoot...
Read more >
Web App root directory - DevExpress Support
My licensing class is used in both a winforms and web XAF app so I've ... string licFile = rootPath + @"\Deep Blue...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found