[QUESTION] How can I use Gunicorns SCRIPT_NAME with FastAPI?
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
Is it possible to allow routes to be prefixed using Gunicorn’s support for the SCRIPT_NAME
env variable? I read the discussion in #461 but could not find any other working solution than manually prepending the prefixed url to each route.
Additional context
What I would like to do is basically have a route prefix by setting:
os.environ["SCRIPT_NAME"] = "/a/b/c/d/e"
and then allowing Gunicorn to route for example
@app.get("/hello")
def read_root():
return {"Hello": "World"}
to /a/b/c/d/e/hello
.
I tried running the Uvicorn + Gunicorn combination using gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
to achieve this. It will simply ignore the prefix and keep on routing to /hello
instead.
Gunicorn will do this out of the box if the env var is present when serving a Flask or Django app.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Server Workers - Gunicorn with Uvicorn - FastAPI
Here I'll show you how to use Gunicorn with Uvicorn worker processes. ... used by Uvicorn, main means the Python module named "...
Read more >In Python + FastAPI, how to access a Gunicorn served website ...
I am not using virtual env. Now I ssh into the VM and cd into the project root folder where the main.py is...
Read more >tiangolo/fastapi - Gitter
Another question, I'm deploying my apps with gunicorn which in principle should allow me to mount the main app using the environment variable...
Read more >FastAPI Tutorial - Building RESTful APIs with Python - YouTube
In this Python tutorial you will learn about FastAPI, a Web framework for developing RESTful APIs in Python. FastAPI is based on Pydantic ......
Read more >tiangolo/uvicorn-gunicorn-fastapi - Docker Image
You can use Gunicorn to start and manage multiple Uvicorn worker processes. That way, you get the best of concurrency and parallelism in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As shown in my proof-of-concept, you still have to subclass the uvicorn worker similar to the following:
@Midnighter I think that’s worth a PR to Uvicorn ✔️