root_path does not suported #2967
-
Title: Static files and admin panel assets not loading correctly behind a proxy with root_path in FastAPI Description: I’m running a FastAPI application behind a reverse proxy (Nginx) and have configured root_path in the FastAPI app as described in the official documentation: However, static files (e.g., CSS, JS) and possibly other paths are not resolving correctly. I’m using starlette-admin, and the admin panel attempts to load assets using paths like: My FastAPI setup:
These static paths return 404 errors. I’ve tried the following without success: Despite all of this, the admin panel fails to load any static assets and cannot function beyond the initial page. I cannot navigate to sub-pages, and CSS/JS files do not load. Example errors: Additionally, without setting root_path, Swagger UI does not work behind a proxy — which is already a critical issue on its own. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
This is 1000% a bug. I work with this admin panel full time for a few years — I know it well. It's far from perfect. I use it in production with static files that I personally modified so they work with HTTPS. I mean, in most situations I prefer to fix things locally by modifying your code. But now I have no clue what exactly to modify. Can you give me a hint on how to fix this locally? For example, by changing |
Beta Was this translation helpful? Give feedback.
-
As a workaround you can try configuring your proxy not to strip prefix from the path. You can read my attempt to break down this issue here: #2931 |
Beta Was this translation helpful? Give feedback.
-
Shoutout to @YuriiMotov !😄 What I changed was the uvicorn startup command: So here’s my final proxy config: location /api/ { admin = Admin(title = 'Code.❤️ Admin', |
Beta Was this translation helpful? Give feedback.
Shoutout to @YuriiMotov !😄
In the end, I created a separate route for the admin like this:
location /admin/ { proxy_pass http://api_upstream; }
But actually, that was already in place before.
What I changed was the uvicorn startup command:
uvicorn main:app *** --forwarded-allow-ips='*' --proxy-headers --root-path /api
As Yury mentioned, if you pass --root-path to Uvicorn and not to FastAPI directly, then this path actually gets passed correctly to FastAPI, and as a result, the admin panel works properly, and the path stays correct — Swagger keeps working.
So here’s my final proxy config:
`
location /admin/ {
proxy_pass http://api_dev_upstream;
proxy_set_header Host $host;
proxy_set_header…