I am using fastapi, gunicorn and nginx on my server
My web application main.py is
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
I run the code above to listen two ports 8080 and 8081
gunicorn -k uvicorn.workers.UvicornWorker --bind "0.0.0.0:8080" --log-level debug main:app
gunicorn -k uvicorn.workers.UvicornWorker --bind "0.0.0.0:8081" --log-level debug main:app
Then in my nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 127.0.0.1;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:8080/;
}
location /another {
proxy_pass http://127.0.0.1:8081/;
}
}
I expect the results like below
xxx.xxx.xxx.xxx/ => {"Hello":"World"}
xxx.xxx.xxx.xxx/items/1 => {"item_id":1,q:null}
xxx.xxx.xxx.xxx/another => {"Hello":"World"}
xxx.xxx.xxx.xxx/another/items/1 => {"item_id":1,q:null}
But I get
xxx.xxx.xxx.xxx/ => {"Hello":"World"}
xxx.xxx.xxx.xxx/items/1 => {"item_id":1,q:null}
xxx.xxx.xxx.xxx/another => {"Hello":"World"}
xxx.xxx.xxx.xxx/another/items/1 => {"Detail": "Not Found"} // Here is the problem
How can I forward the request as I expect?